human
Find four elements that sum to a given value | Set 2 | Function to find 4 elements that add up to given sum.
Iterate from 0 to temp . length.
Iterate from 0 to length of arr.
Iterate from i + 1 to length of arr.
Store curr_sum = arr [ i ] + arr [ j ].
Check if X - curr_sum if present in map.
Store pair having map value X - curr_sum.
Print the output.
Function for two Sum.
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 fourSum ( X , arr , Map , N ) :
	temp = [ 0 for i in range ( N ) ]
	for i in range ( N - 1 ) :
		for j in range ( i + 1 , N ) :
			curr_sum = arr [ i ] + arr [ j ]
			if ( X - curr_sum ) in Map :
				p = Map [ X - curr_sum ]
				if ( p [ 0 ] != i and p [ 1 ] != i and p [ 0 ] != j and p [ 1 ] != j and temp [ p [ 0 ] ] == 0 and temp [ p [ 1 ] ] == 0 and temp [ i ] == 0 and temp [ j ] == 0 ) :
					print ( arr [ i ] , " , " , arr [ j ] , " , " , arr [ p [ 0 ] ] , " , " , arr [ p [ 1 ] ] , sep = " " )
					temp [ p [ 1 ] ] = 1
					temp [ i ] = 1
					temp [ j ] = 1
					break
def twoSum ( nums , N ) :
	Map = { }
	for i in range ( N - 1 ) :
		for j in range ( i + 1 , N ) :
			Map [ nums [ i ] + nums [ j ] ] = [ ]
			Map [ nums [ i ] + nums [ j ] ] . append ( i )
			Map [ nums [ i ] + nums [ j ] ] . append ( j )
	return Map
arr = [ 10 , 20 , 30 , 40 , 1 , 2 ]
n = len ( arr )
X = 91
Map = twoSum ( arr , n )
fourSum ( X , arr , Map , n )