Find three element from different three arrays such that a + b + c = sum | C # 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 . ; Store elements of first array in hash ; sum last two arrays element one by one ; Consider current pair and find if there is an element in a1 [ ] such that these three form a required triplet ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static bool findTriplet ( int [ ] a1 , int [ ] a2 , int [ ] a3 , int n1 , int n2 , int n3 , int sum ) { HashSet < int > s = new HashSet < int > ( ) ; for ( int i = 0 ; i < n1 ; i ++ ) { s . Add ( a1 [ i ] ) ; } List < int > al = new List < int > ( s ) ; for ( int i = 0 ; i < n2 ; i ++ ) { for ( int j = 0 ; j < n3 ; j ++ ) { if ( al . Contains ( sum - a2 [ i ] - a3 [ j ] ) & al . IndexOf ( sum - a2 [ i ] - a3 [ j ] ) != al [ al . Count - 1 ] ) { return true ; } } } return false ; } 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 " ) ; } } }