human
Find four elements that sum to a given value | Set 2 | The function finds four elements with given summ X.
Store summs of all pairs in a hash table.
Traverse through all pairs and search for X - ( current pair summ ) ..
If X - summ is present in hash table ,.
Making sure that all elements are distinct array elements and an element is not considered more than once ..
Driver code.
Function call"
Solution in Python:
assistant
#Here is code to find four elements that sum to a given value and the other steps required.
 def findFourElements ( arr , n , X ) :
	mp = { }
	for i in range ( n - 1 ) :
		for j in range ( i + 1 , n ) :
			mp [ arr [ i ] + arr [ j ] ] = [ i , j ]
	for i in range ( n - 1 ) :
		for j in range ( i + 1 , n ) :
			summ = arr [ i ] + arr [ j ]
			if ( X - summ ) in mp :
				p = mp [ X - summ ]
				if ( p [ 0 ] != i and p [ 0 ] != j and p [ 1 ] != i and p [ 1 ] != j ) :
					print ( arr [ i ] , " , ▁ " , arr [ j ] , " , ▁ " , arr [ p [ 0 ] ] , " , ▁ " , arr [ p [ 1 ] ] , sep = " " )
					return
arr = [ 10 , 20 , 30 , 40 , 1 , 2 ]
n = len ( arr )
X = 91
findFourElements ( arr , n , X )