在此问题中,给定3个数组X,Y,Z。我们的任务是创建一个程序,以查找包含3个数组中的元素的特殊三元组的和。
特殊三元组是具有以下属性的特殊三元组类型-
对于(a,b,c):a≤b且b≥c ,即三元组的中间元素应比其他两个元素要好。
并且,三元组的值由公式-
f(a, b, c) = (a+b) * (b+c)
要创建此三元组,我们需要使用给定的三个数组相互使用一个元素。
让我们举个例子来了解这个问题,
输入-
X[] = {5, 9, 4} ; Y[] = {8, 6} ; Z[] = {7, 1}
输出结果
说明-我们找到所有特殊三元组的值。
(5, 8, 7) : value = (5+8) * (8+7) = 195 (5, 8, 1) : value = (5+8) * (8+1) = 117 (4, 8, 7) : value = (4+8) * (8+7) = 180 (4, 8, 1) : value = (4+8) * (8+1) = 108 (5, 6, 1) : value = (5+6) * (6+1) = 77 (4, 6, 1) : value = (4+6) * (6+1) = 70 Sum of special triplets = 747
解决此问题的一种简单方法是从阵列生成所有三元组。对于所有特殊三元组,请使用上述公式计算其值。然后将它们添加到sum变量并返回最终的sum。
用于说明解决方案的程序,
#include <iostream> using namespace std; int findSpecialTripletSum(int X[], int Y[], int Z[], int sizeX, int sizeY, int sizeZ) { int sum = 0; for (int i = 0; i < sizeX; i++) { for (int j = 0; j < sizeY; j++) { for (int k = 0; k < sizeZ; k++) { if (X[i] <= Y[j] && Z[k] <= Y[j]) sum = sum + (X[i] + Y[j]) * (Y[j] + Z[k]); } } } return sum; } int main() { int X[] = {5, 9, 4}; int Y[] = {8, 6}; int Z[] = {7, 1}; int sizeX = sizeof(X) / sizeof(X[0]); int sizeY = sizeof(Y) / sizeof(Y[0]); int sizeZ = sizeof(Z) / sizeof(Z[0]); cout<<"Sum of special triplets = "<<findSpecialTripletSum(X, Y, Z, sizeX, sizeY, sizeZ); }
输出结果
Sum of special triplets = 747
另一个更有效的解决方案是对数组X和Z进行排序。然后检查满足数组Y的每个元素的特殊三元组要求的元素。
因此,对于数组Y的索引i处的任何元素,即Y [i]。数组X {x1,x2}和Z {z1,z2}的元素小于Y [i],则
值的总和
S = (x1+Y[i])(Y[i]+z1) + (x1+Y[i])(Y[i]+z2) + (x2+Y[i])(Y[i]+z1) + (x2+Y[i])(Y[i]+z2) S = (x1+Y[i])(Y[i]+z1+Y[i]+z2) + (x2+Y[i])(Y[i]+z1+Y[i]+z2) S = (2Y[i] + x1 + x2)(2y[i] + z1 + z2)
N = X中大于Y [i]的元素数量
M = Z中大于Y [i]的元素数量
Sx = X中大于Y [i]的元素之和
Sz = Z中大于Y [i]的元素之和
S = (N*Y[i] + Sx) * (M*Y[i] + Sz)
用于说明上述解决方案的程序,
#include <bits/stdc++.h> using namespace std; int tripletSumCalc(int X[], int Y[], int Z[], int prefixSumA[], int prefixSumC[], int sizeA, int sizeB, int sizeC){ int totalSum = 0; for (int i = 0; i < sizeB; i++) { int currentElement = Y[i]; int n = upper_bound(X, X + sizeA, currentElement) - X; int m = upper_bound(Z, Z + sizeC, currentElement) - Z; if (n == 0 || m == 0) continue; totalSum += ((prefixSumA[n - 1] + (n * currentElement)) * (prefixSumC[m - 1] + (m * currentElement))); } return totalSum; } int* findPrefixSum(int* arr, int n) { int* prefixSumArr = new int[n]; prefixSumArr[0] = arr[0]; for (int i = 1; i < n; i++) prefixSumArr[i] = prefixSumArr[i - 1] + arr[i]; return prefixSumArr; } int findSpecialTripletSum(int A[], int B[], int C[], int sizeA, int sizeB, int sizeC){ int specialTripletSum = 0; sort(A, A + sizeA); sort(C, C + sizeC); int* prefixSumA = findPrefixSum(A, sizeA); int* prefixSumC = findPrefixSum(C, sizeC); return tripletSumCalc(A, B, C, prefixSumA, prefixSumC, sizeA, sizeB, sizeC); } int main() { int A[] = {5, 9, 4}; int B[] = {8, 6}; int C[] = {7, 1}; int sizeA = sizeof(A) / sizeof(A[0]); int sizeB = sizeof(B) / sizeof(B[0]); int sizeC = sizeof(C) / sizeof(C[0]); cout<<"Sum of special triplets = "<<findSpecialTripletSum(A, B, C, sizeA, sizeB, sizeC); }
输出结果
Sum of special triplets = 747