Maximize XOR by selecting 3 numbers in range [ 0 , A ] , [ 0 , B ] , and [ 0 , C ] respectively | C ++ Program to implement the above approach ; 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maximumTripletXOR ( int A , int B , int C ) { int ans = 0 ; for ( int i = 30 ; i >= 0 ; i -- ) { int cur = 1 << i ; if ( A >= cur ) { ans += cur ; A -= cur ; } else if ( B >= cur ) { ans += cur ; B -= cur ; } else if ( C >= cur ) { ans += cur ; C -= cur ; } } return ans ; } int main ( ) { int A = 6 ; int B = 2 ; int C = 10 ; cout << maximumTripletXOR ( A , B , C ) ; }