Maximize XOR by selecting 3 numbers in range [ 0 , A ] , [ 0 , B ] , and [ 0 , C ] respectively | Function to calculate maximum triplet XOR form 3 ranges ; Initialize a variable to store the answer ; create minimum number that have a set bit at ith position ; Check for each number and try to greedily choose the bit if possible If A >= cur then A also have a set bit at ith position ; Increment the answer ; Subtract this value from A ; Check for B ; Increment the answer ; Subtract this value from B ; Check for C ; Increment the answer ; Subtract this value from C ; If any of the above conditions is not satisfied then there is no way to turn that bit ON . ; Driver Code
def maximumTripletXOR ( A , B , C ) : NEW_LINE INDENT ans = 0 NEW_LINE for i in range ( 30 , - 1 , - 1 ) : NEW_LINE INDENT cur = 1 << i NEW_LINE if ( A >= cur ) : NEW_LINE INDENT ans += cur NEW_LINE A -= cur NEW_LINE DEDENT elif ( B >= cur ) : NEW_LINE INDENT ans += cur NEW_LINE B -= cur NEW_LINE DEDENT elif ( C >= cur ) : NEW_LINE INDENT ans += cur NEW_LINE C -= cur NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT A = 6 NEW_LINE B = 2 NEW_LINE C = 10 NEW_LINE print ( maximumTripletXOR ( A , B , C ) ) NEW_LINE