Sum of special triplets having elements from 3 arrays in C++


In this problem, we are given 3 array X, Y, Z. Our task is to create a program to find the Sum of special triplets having elements from 3 arrays.

Special Triplet is a special type of triplet that hold the following property −

For (a, b, c): a ≤ b and b ≥ c, i.e the middle element of the triplet should be greeter that the other two.

And, the value of the triplet is given by the formula −

f(a, b, c) = (a+b) * (b+c)

To create this triplet we need to use one element from each other the three arrays given.

Let’s take an example to understand the problem,

Input

X[] = {5, 9, 4} ; Y[] = {8, 6} ; Z[] = {7, 1}

Output

Explanation − Let’s find the values of all special triplets.

(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

A simple solution to this problem is generating all triplets from the array. For all special triplets, calculating its value using the above formula. And then add them to a sum variable and return the final sum.

Example

Program to illustrate the solution,

 Live Demo

#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);
}

Output

Sum of special triplets = 747

Another more efficient solution could be by sorting array X and Z. Then check for the elements that meet the special triplets requirement for each element of array Y.

So, for any element at index i of array Y i.e. Y[i]. The elements of the array X {x1, x2} and Z {z1, z2}, are less than Y[i], then

The sum of values,

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 = number of elements greater than Y[i] in X

M = number of elements greater than Y[i] in Z

Sx = sum of elements greater than Y[i] in X

Sz = sum of elements greater than Y[i] in Z

S = (N*Y[i] + Sx) * (M*Y[i] + Sz)

Example

Program to illustrate the above solution,

 Live Demo

#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);
}

Output

Sum of special triplets = 747

Updated on: 05-Aug-2020

90 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements