Find three element from different three arrays such that that a + b + c = sum

Given three integer arrays and a “sum”, the task is to check if there are three elements a, b, c such that a + b + c = sum and a, b and c belong to three different arrays.

Examples:

Input : a1[] = { 1 , 2 , 3 , 4 , 5 };
	a2[] = { 2 , 3 , 6 , 1 , 2 };
	a3[] = { 3 , 2 , 4 , 5 , 6 };  
        sum = 9
Output : Yes
1  + 2  + 6 = 9  here 1 from a1[] and 2 from
a2[] and 6 from a3[]   
    
Input : a1[] = { 1 , 2 , 3 , 4 , 5 };
	a2[] = { 2 , 3 , 6 , 1 , 2 };
	a3[] = { 3 , 2 , 4 , 5 , 6 };   
         sum = 20 
Output : No

A naive approach is to run three loops and check sum of three element form different arrays equal to given number if find then print exist and otherwise print not exist.

// C++ program to find three element from different
// three arrays such that that a + b + c is equal
// to given sum
#include<bits/stdc++.h>
using namespace std;

// Function to check if there is an element from
// each array such that sum of the three elements
// is equal to given sum.
bool findTriplet(int a1[], int a2[], int a3[],
                 int n1, int n2, int n3, int sum)
{
    for (int i=0; i<n1; i++)
      for (int j=0; j<n2; j++)
         for (int k=0; k<n3; k++)
            if (a1[i] + a2[j] + a3[k] == sum)
               return true;

    return false;
}

// Driver Code
int main()
{
    int a1[] = { 1 , 2 , 3 , 4 , 5 };
    int a2[] = { 2 , 3 , 6 , 1 , 2 };
    int a3[] = { 3 , 2 , 4 , 5 , 6 };
    int sum = 9;
    int n1 = sizeof(a1)/sizeof(a1[0]);
    int n2 = sizeof(a2)/sizeof(a2[0]);
    int n3 = sizeof(a3)/sizeof(a3[0]);
    findTriplet(a1, a2, a3, n1, n2, n3, sum)?
                 cout << "Yes" : cout << "No";
    return 0;
}

Output:

Yes

Time complexity : O(n3)
Space complexity : O(1)

 

An efficient solution is to store all elements of first array in hash table (unordered_set in C++) and calculate sum of two elements last two array elements one by one and substract from given number k and check in hash table if it’s exist in hash table then print exist and otherwise not exist.

1. Store all elements of first array in hash table
2. Generate all pairs of elements from two arrays using
   nested loop. For every pair (a1[i], a2[j]), check if
   sum - (a1[i] + a2[j]) exists in hash table. If yes
   return true.      

Below is C++ implementation of above idea.

// C++ program to find three element from different
// three arrays such that that a + b + c is equal
// to given sum
#include<bits/stdc++.h>
using namespace std;

// Function to check if there is an element from
// each array such that sum of the three elements
// is equal to given sum.
bool findTriplet(int a1[], int a2[], int a3[],
              int n1, int n2, int n3, int sum)
{
    // Store elements of first array in hash
    unordered_set <int> s;
    for (int i=0; i<n1; i++)
        s.insert(a1[i]);

    // sum last two arrays element one by one
    for (int i=0; i<n2; i++)
    {
        for (int j=0; j<n3; j++)
        {
            // Consider current pair and find if there
            // is an element in a1[] such that these
            // three form a required triplet
            if (s.find(sun - a2[i] - a3[j]) != s.end())
                 return true;
        }
    }

    return false;
}

// Driver Code
int main()
{
    int a1[] = { 1 , 2 , 3 , 4 , 5 };
    int a2[] = { 2 , 3 , 6 , 1 , 2 };
    int a3[] = { 3 , 2 , 4 , 5 , 6 };
    int sum = 9;
    int n1 = sizeof(a1)/sizeof(a1[0]);
    int n2 = sizeof(a2)/sizeof(a2[0]);
    int n3 = sizeof(a3)/sizeof(a3[0]);
    findTriplet(a1, a2, a3, n1, n2, n3, sum)?
    cout << "Yes" : cout << "No";

    return 0;
}

Output:

Yes

Time complexity : O(n2)
Auxiliary Space : O(n)

References :
http://stackoverflow.com/questions/2070359/finding-three-elements-in-an-array-whose-sum-is-closest-to-a-given-number

This article is contributed by DANISH_RAZA 🙂 . If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

GATE CS Corner    Company Wise Coding Practice





Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.