Minimum insertions to make XOR of an Array equal to half of its sum | Function to make XOR of the array equal to half of its sum ; Calculate the sum and Xor of all the elements ; If the required condition satisfies already , return the original array ; If Xor is already zero , Insert sum ; Otherwise , insert xr and insert sum + xr ; Driver code
def make_xor_half ( arr ) : NEW_LINE INDENT sum = 0 ; xr = 0 ; NEW_LINE for a in arr : NEW_LINE INDENT sum += a ; NEW_LINE xr ^= a ; NEW_LINE DEDENT if ( 2 * xr == sum ) : NEW_LINE INDENT return - 1 ; NEW_LINE DEDENT if ( xr == 0 ) : NEW_LINE INDENT arr . append ( sum ) ; NEW_LINE return 1 ; NEW_LINE DEDENT arr . append ( xr ) ; NEW_LINE arr . append ( sum + xr ) ; NEW_LINE return 2 ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 7 ; NEW_LINE nums = [ 3 , 4 , 7 , 1 , 2 , 5 , 6 ] ; NEW_LINE count = make_xor_half ( nums ) ; NEW_LINE if ( count == - 1 ) : NEW_LINE INDENT print ( " - 1" ) ; NEW_LINE DEDENT elif ( count == 1 ) : NEW_LINE INDENT print ( nums [ N ] ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( nums [ N ] , nums [ N + 1 ] ) ; NEW_LINE DEDENT DEDENT