Find three element from different three arrays such 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.
Algorithm:
- Define a function findTriplet that takes in three integer arrays a1, a2, a3, their respective sizes n1, n2, n3, and the integer variable sum.
- Loop through each element of a1, a2, and a3 using three nested for-loops.
- If the sum of the current triplet of elements (a1[i], a2[j], a3[k]) is equal to sum, return true.
- If no such triplet is found, return false.
- In the main function, define three integer arrays a1, a2, a3, and the integer variable sum.
- Get the size of each array using sizeof operator and divide it by the size of a single element to get the size of the array in terms of number of elements.
- Call the findTriplet function with a1, a2, a3, n1, n2, n3, and sum as arguments.
- If the function returns true, print “Yes“. Otherwise, print “No“.
Implementation:
C++
// C++ program to find three element // from different three arrays such // 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; } |
Java
// Java program to find three element // from different three arrays such // that a + b + c is equal to // given sum class GFG { // Function to check if there is // an element from each array such // that sum of the three elements // is equal to given sum. static boolean 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 public static void main (String[] args) { 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 = a1.length; int n2 = a2.length; int n3 = a3.length; if (findTriplet(a1, a2, a3, n1, n2, n3, sum)) System.out.print( "Yes" ); else System.out.print( "No" ); } } // This code is contributed by Anant Agarwal. |
Python3
# Python3 program to find # three element from different # three arrays such that # a + b + c is equal to # given sum # Function to check if there # is an element from each # array such that sum of the # three elements is equal to # given sum. def findTriplet(a1, a2, a3, n1, n2, n3, sum ): for i in range ( 0 , n1): for j in range ( 0 , n2): for k in range ( 0 , n3): if (a1[i] + a2[j] + a3[k] = = sum ): return True return False # Driver Code a1 = [ 1 , 2 , 3 , 4 , 5 ] a2 = [ 2 , 3 , 6 , 1 , 2 ] a3 = [ 3 , 2 , 4 , 5 , 6 ] sum = 9 n1 = len (a1) n2 = len (a2) n3 = len (a3) print ( "Yes" ) if findTriplet(a1, a2, a3, n1, n2, n3, sum ) else print ( "No" ) # This code is contributed # by Smitha |
C#
// C# program to find three element // from different three arrays such // that a + b + c is equal to // given sum using System; public class GFG { // Function to check if there is an // element from each array such that // sum of the three elements is // equal to given sum. static 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 static public void 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 = a1.Length; int n2 = a2.Length; int n3 = a3.Length; if (findTriplet(a1, a2, a3, n1, n2, n3, sum)) Console.WriteLine( "Yes" ); else Console.WriteLine( "No" ); } } // This code is contributed by vt_m. |
PHP
<?php // PHP program to find three element // from different three arrays such // that a + b + c is equal to // given sum // Function to check if there is an // element from each array such that // sum of the three elements is equal // to given sum. function findTriplet( $a1 , $a2 , $a3 , $n1 , $n2 , $n3 , $sum ) { for ( $i = 0; $i < $n1 ; $i ++) for ( $j = 0; $j < $n2 ; $j ++) for ( $k = 0; $k < $n3 ; $k ++) if ( $a1 [ $i ] + $a2 [ $j ] + $a3 [ $k ] == $sum ) return true; return false; } // Driver Code $a1 = array ( 1 , 2 , 3 , 4 , 5 ); $a2 = array ( 2 , 3 , 6 , 1 , 2 ); $a3 = array ( 3 , 2 , 4 , 5 , 6 ); $sum = 9; $n1 = count ( $a1 ); $n2 = count ( $a2 ); $n3 = count ( $a3 ); if (findTriplet( $a1 , $a2 , $a3 , $n1 , $n2 , $n3 , $sum )==true) echo "Yes" ; else echo "No" ; // This code is contributed by anuj_67. ?> |
Javascript
<script> // JavaScript program to find three element // from different three arrays such // that a + b + c is equal to // given sum // Function to check if there is // an element from each array such // that sum of the three elements // is equal to given sum. function findTriplet(a1, a2, a3, n1, n2, n3, sum) { for ( var i = 0; i < n1; i++) for ( var j = 0; j < n2; j++) for ( var k = 0; k < n3; k++) if (a1[i] + a2[j] + a3[k] == sum) return true ; return false ; } // Driver Code var a1 = [ 1 , 2 , 3 , 4 , 5 ]; var a2 = [ 2 , 3 , 6 , 1 , 2 ]; var a3 = [ 3 , 2 , 4 , 5 , 6 ]; var sum = 9; var n1 = a1.length; var n2 = a2.length; var n3 = a3.length; findTriplet(a1, a2, a3, n1, n2, n3, sum)? document.write( "Yes" ) : document.write( "No" ); </script> |
Yes
Time Complexity : O(n3)
Auxiliary Space: 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 subtract from given number k and check in hash table if it exists in the 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 the implementation of above idea.
C++
// C++ program to find three element // from different three arrays such // 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(sum - 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; } |
Java
// Java program to find three element // from different three arrays such // that a + b + c is equal to // given sum import java.util.*; class GFG { // Function to check if there is // an element from each array such // that sum of the three elements is // equal to given sum. static boolean findTriplet( int a1[], int a2[], int a3[], int n1, int n2, int n3, int sum) { // Store elements of // first array in hash HashSet<Integer> s = new HashSet<Integer>(); for ( int i = 0 ; i < n1; i++) { s.add(a1[i]); } // sum last two arrays // element one by one ArrayList<Integer> al = new ArrayList<>(s); 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 (al.contains(sum - a2[i] - a3[j]) & al.indexOf(sum - a2[i] - a3[j]) != al.get(al.size() - 1 )) { return true ; } } } return false ; } // Driver Code public static void main(String[] args) { 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 = a1.length; int n2 = a2.length; int n3 = a3.length; if (findTriplet(a1, a2, a3, n1, n2, n3, sum)) { System.out.println( "Yes" ); } else { System.out.println( "No" ); } } } // This code is contributed by 29AjayKumar |
Python3
# Python3 program to find three element # from different three arrays such # that a + b + c is equal to # given sum # Function to check if there is # an element from each array such # that sum of the three elements is # equal to given sum. def findTriplet(a1, a2, a3, n1, n2, n3, sum ): # Store elements of first # array in hash s = set () # sum last two arrays element # one by one for i in range (n1): s.add(a1[i]) for i in range (n2): for j in range (n3): # Consider current pair and # find if there is an element # in a1[] such that these three # form a required triplet if sum - a2[i] - a3[j] in s: return True return False # Driver code a1 = [ 1 , 2 , 3 , 4 , 5 ] a2 = [ 2 , 3 , 6 , 1 , 2 ] a3 = [ 3 , 24 , 5 , 6 ] n1 = len (a1) n2 = len (a2) n3 = len (a3) sum = 9 if findTriplet(a1, a2, a3, n1, n2, n3, sum ) = = True : print ( "Yes" ) else : print ( "No" ) # This code is contributed by Shrikant13 |
C#
// C# program to find three element // from different three arrays such // that a + b + c is equal to // given sum using System; using System.Collections.Generic; class GFG { // Function to check if there is // an element from each array such // that sum of the three elements is // equal to given sum. static bool findTriplet( int []a1, int []a2, int []a3, int n1, int n2, int n3, int sum) { // Store elements of // first array in hash HashSet< int > s = new HashSet< int >(); for ( int i = 0; i < n1; i++) { s.Add(a1[i]); } // sum last two arrays // element one by one List< int > al = new List< int >(s); 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 (al.Contains(sum - a2[i] - a3[j]) & al.IndexOf(sum - a2[i] - a3[j]) != al[al.Count - 1]) { return true ; } } } return false ; } // Driver Code public static void Main(String[] args) { 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 = a1.Length; int n2 = a2.Length; int n3 = a3.Length; if (findTriplet(a1, a2, a3, n1, n2, n3, sum)) { Console.WriteLine( "Yes" ); } else { Console.WriteLine( "No" ); } } } // This code is contributed by PrinciRaj1992 |
Javascript
<script> // Javascript program to find three element // from different three arrays such // that a + b + c is equal to // given sum // Function to check if there is // an element from each array such // that sum of the three elements is // equal to given sum. function findTriplet(a1, a2, a3, n1, n2, n3, sum) { // Store elements of // first array in hash var s = new Set(); for ( var i = 0; i < n1; i++) s.add(a1[i]); // sum last two arrays // element one by one for ( var i = 0; i < n2; i++) { for ( var 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.has(sum - a2[i] - a3[j])) return true ; } } return false ; } // Driver Code var a1 = [1 , 2 , 3 , 4 , 5]; var a2 = [2 , 3 , 6 , 1 , 2]; var a3 = [3 , 2 , 4 , 5 , 6]; var sum = 9; var n1 = a1.length; var n2 = a2.length; var n3 = a3.length; findTriplet(a1, a2, a3, n1, n2, n3, sum)? document.write( "Yes" ): document.write( "No" ); // This code is contributed by famously. </script> |
Yes
Time Complexity: O(n2)
Auxiliary Space: O(n)
Another efficient approach ( Space optimization ) : we will run two loops, then we will search for required sum in third loop using binary search .
Below is the implementation of the above approach :
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; //Function to check if x is present in the array or not bool binarysearch( int arr[], int N, int x) { int l = 0, r = N - 1; while (l <= r) { int mid = (l + r) / 2; // Checking if the middle element is equal to x if (arr[mid] == x) { return true ; } else if (arr[mid] < x) { l = mid + 1; } else { r = mid - 1; } } // return true , if element x is present in the array // else false return false ; } // Function to check is such triplet(a+b+c=sum) exist that bool findTriplet( int a1[], int a2[], int a3[], int n1, int n2, int n3, int sum) { sort(a3,a3+n3); //sort third array(a3) in ascending order // for binary search // Iterate each element of array a1 for ( int i = 0; i < n1; i++) { // Iterate each element of array a2 for ( int j = 0; j < n2; j++) { int requiredsum= sum-a1[i]-a2[j]; if (binarysearch(a3, n3,requiredsum)) { return true ;; //return true if Triplet exist } } } return false ; //return false if Triplet doesn't exist } // 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 ( int ); int n2 = sizeof (a2) / sizeof ( int ); int n3 = sizeof (a3) / sizeof ( int ); //Function call if (findTriplet(a1, a2, a3, n1, n2, n3,sum)) { cout<< "YES" <<endl; //if Triplet exist , print YEs } else { cout<< "NO" <<endl; // else NO } return 0; } // This Approach is contributed by nikhilsainiofficial546 |
Java
import java.util.Arrays; class Main { // Function to check if x is present in the array or not static boolean binarysearch( int arr[], int N, int x) { int l = 0 , r = N - 1 ; while (l <= r) { int mid = (l + r) / 2 ; // Checking if the middle element is equal to x if (arr[mid] == x) { return true ; } else if (arr[mid] < x) { l = mid + 1 ; } else { r = mid - 1 ; } } // return true , if element x is present in the // array else false return false ; } // Function to check is such triplet(a+b+c=sum) exist // that static boolean findTriplet( int a1[], int a2[], int a3[], int n1, int n2, int n3, int sum) { Arrays.sort( a3); // sort third array(a3) in ascending order // for binary search // Iterate each element of array a1 for ( int i = 0 ; i < n1; i++) { // Iterate each element of array a2 for ( int j = 0 ; j < n2; j++) { int requiredsum = sum - a1[i] - a2[j]; if (binarysearch(a3, n3, requiredsum)) { return true ; // return true if Triplet // exist } } } return false ; // return false if Triplet doesn't // exist } // Driver Code public static void main(String[] args) { 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 = a1.length; int n2 = a2.length; int n3 = a3.length; // Function call if (findTriplet(a1, a2, a3, n1, n2, n3, sum)) { System.out.println( "YES" ); // if Triplet exist , print YEs } else { System.out.println( "NO" ); // else NO } } } //This code is Contributed by chinmaya121221 |
Python3
# Function to check if x is present in the array or not def binarysearch(arr, N, x): l = 0 r = N - 1 while l < = r: mid = (l + r) / / 2 # Checking if the middle element is equal to x if arr[mid] = = x: return True elif arr[mid] < x: l = mid + 1 else : r = mid - 1 # return True, if element x is present in the array # else False return False # Function to check if such triplet (a + b + c = sum) exists def findTriplet(a1, a2, a3, n1, n2, n3, sum ): a3.sort() # sort third array(a3) in ascending order for binary search # Iterate each element of array a1 for i in range (n1): # Iterate each element of array a2 for j in range (n2): requiredsum = sum - a1[i] - a2[j] if binarysearch(a3, n3, requiredsum): return True # return True if Triplet exists return False # return False if Triplet doesn't exist # Driver code if __name__ = = '__main__' : a1 = [ 1 , 2 , 3 , 4 , 5 ] a2 = [ 2 , 3 , 6 , 1 , 2 ] a3 = [ 3 , 2 , 4 , 5 , 6 ] sum = 9 n1 = len (a1) n2 = len (a2) n3 = len (a3) # Function call if findTriplet(a1, a2, a3, n1, n2, n3, sum ): print ( "YES" ) # if Triplet exists, print YES else : print ( "NO" ) # else NO |
C#
using System; using System.Linq; class Program { // Function to check if x is present in the array or not static bool BinarySearch( int [] arr, int n, int x) { int l = 0, r = n - 1; while (l <= r) { int mid = (l + r) / 2; // Checking if the middle element is equal to x if (arr[mid] == x) { return true ; } else if (arr[mid] < x) { l = mid + 1; } else { r = mid - 1; } } // return true , if element x is present in the array // else false return false ; } // Function to check if such triplet (a+b+c=sum) exists static bool FindTriplet( int [] a1, int [] a2, int [] a3, int n1, int n2, int n3, int sum) { Array.Sort(a3); // sort third array(a3) in ascending order for binary search // Iterate each element of array a1 for ( int i = 0; i < n1; i++) { // Iterate each element of array a2 for ( int j = 0; j < n2; j++) { int requiredSum = sum - a1[i] - a2[j]; if (BinarySearch(a3, n3, requiredSum)) { return true ; // return true if Triplet exists } } } return false ; // return false if Triplet doesn't exist } // Driver Code static void Main( string [] args) { 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 = a1.Length; int n2 = a2.Length; int n3 = a3.Length; // Function call if (FindTriplet(a1, a2, a3, n1, n2, n3, sum)) { Console.WriteLine( "YES" ); // if Triplet exists, print YES } else { Console.WriteLine( "NO" ); // else NO } } } |
Javascript
// JavaScript program for the above approach // Function to check if x is present in the array or not function binarysearch(arr, N, x) { let l = 0; let r = N - 1; while (l <= r) { let mid = Math.floor((l + r) / 2); // Checking if the middle element is equal to x if (arr[mid] === x) { return true ; } else if (arr[mid] < x) { l = mid + 1; } else { r = mid - 1; } } // return true, if element x is present in the array // else false return false ; } // Function to check if such triplet (a + b + c = sum) exists function findTriplet(a1, a2, a3, n1, n2, n3, sum) { a3.sort(); // sort third array(a3) in ascending order for binary search // Iterate each element of array a1 for (let i = 0; i < n1; i++) { // Iterate each element of array a2 for (let j = 0; j < n2; j++) { let requiredsum = sum - a1[i] - a2[j]; if (binarysearch(a3, n3, requiredsum)) { return true ; // return true if Triplet exists } } } return false ; // return false if Triplet doesn't exist } // Driver code const a1 = [1, 2, 3, 4, 5]; const a2 = [2, 3, 6, 1, 2]; const a3 = [3, 2, 4, 5, 6]; const sum = 9; const n1 = a1.length; const n2 = a2.length; const n3 = a3.length; // Function call if (findTriplet(a1, a2, a3, n1, n2, n3, sum)) { console.log( "YES" ); // if Triplet exists, print YES } else { console.log( "NO" ); // else NO } // Contributed by adityasharmadev01 |
YES
Time Complexity: O(n1*n2*log n3)
Auxiliary Space: O(1)