Minimum value to be added to maximize Bitwise XOR of the given array | Python program for the above approach ; Function to find complement of an integer ; Count the number of bits of maxElement ; Return 1 's complement ; Function to find the value required to be added to maximize XOR of the given array ; Stores the required value to be added to the array ; Stores the maximum array element ; Traverse the array ; Update XOR of the array ; Find maximum element in array ; Calculate 1 s ' complement ; Return the answer ; Driver Code
import math NEW_LINE def onesComplement ( n , maxElement ) : NEW_LINE INDENT bits = math . floor ( math . log2 ( maxElement ) ) + 1 NEW_LINE return ( ( 1 << bits ) - 1 ) ^ n NEW_LINE DEDENT def findNumber ( arr , n ) : NEW_LINE INDENT res = 0 NEW_LINE maxElement = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT res = res ^ arr [ i ] NEW_LINE if ( maxElement < arr [ i ] ) : NEW_LINE INDENT maxElement = arr [ i ] NEW_LINE DEDENT DEDENT res = onesComplement ( res , maxElement ) NEW_LINE return ( res ) NEW_LINE DEDENT arr = [ 1 , 2 , 3 , 4 , 5 ] NEW_LINE N = len ( arr ) NEW_LINE print ( findNumber ( arr , N ) ) NEW_LINE