human
Tug of War | function that tries every possible solution by calling itself recursively.
checks whether the it is going out of bound.
checks that the numbers of elements left are not less than the number of elements required to form the solution.
consider the cases when current element is not included in the solution.
add the current element to the solution.
checks if a solution is formed.
checks if the solution formed is better than the best solution so far.
consider the cases where current element is included in the solution.
removes current element before returning to the caller of this function.
main function that generate an arr.
the boolean array that contains the inclusion and exclusion of an element in current set . The number excluded automatically form the other set.
The inclusion / exclusion array for final solution.
Find the solution using recursive function TOWUtil ( ).
Print the solution.
Driver Code"
Solution in Python:
 Give me a code to tug of War and the other steps required?
assistant
#Here is code to tug of War and the other steps required.
 def TOWUtil ( arr , n , curr_elements , no_of_selected_elements , soln , min_diff , Sum , curr_sum , curr_position ) :
	if ( curr_position == n ) :
		return
	if ( ( int ( n / 2 ) - no_of_selected_elements ) > ( n - curr_position ) ) :
		return
	TOWUtil ( arr , n , curr_elements , no_of_selected_elements , soln , min_diff , Sum , curr_sum , curr_position + 1 )
	no_of_selected_elements += 1
	curr_sum = curr_sum + arr [ curr_position ]
	curr_elements [ curr_position ] = True
	if ( no_of_selected_elements == int ( n / 2 ) ) :
		if ( abs ( int ( Sum / 2 ) - curr_sum ) < min_diff [ 0 ] ) :
			min_diff [ 0 ] = abs ( int ( Sum / 2 ) - curr_sum )
			for i in range ( n ) :
				soln [ i ] = curr_elements [ i ]
	else :
		TOWUtil ( arr , n , curr_elements , no_of_selected_elements , soln , min_diff , Sum , curr_sum , curr_position + 1 )
	curr_elements [ curr_position ] = False
def tugOfWar ( arr , n ) :
	curr_elements = [ None ] * n
	soln = [ None ] * n
	min_diff = [ 999999999999 ]
	Sum = 0
	for i in range ( n ) :
		Sum += arr [ i ]
		curr_elements [ i ] = soln [ i ] = False
	TOWUtil ( arr , n , curr_elements , 0 , soln , min_diff , Sum , 0 , 0 )
	print ( " The ▁ first ▁ subset ▁ is : ▁ " )
	for i in range ( n ) :
		if ( soln [ i ] == True ) :
			print ( arr [ i ] , end = " ▁ " )
	print ( )
	print ( " The ▁ second ▁ subset ▁ is : ▁ " )
	for i in range ( n ) :
		if ( soln [ i ] == False ) :
			print ( arr [ i ] , end = " ▁ " )
if __name__ == ' _ _ main _ _ ' :
	arr = [ 23 , 45 , - 34 , 12 , 0 , 98 , - 99 , 4 , 189 , - 1 , 4 ]
	n = len ( arr )
	tugOfWar ( arr , n )