Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Maximize XOR by selecting 3 numbers in range [0, A], [0, B], and [0, C] respectively

  • Difficulty Level : Medium
  • Last Updated : 17 Aug, 2021

Given 3 integers A, B, C, the task is to find the maximum XOR value of three number selected one from each range [0, A], [0, B], [0, C] respectively.

Example: 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

Input: A = 1, B = 2, C = 4
Output: 7
Explanation: Maximum XOR can be calculated by selecting 1 (from [0, 1]), 2 (from [0, 2]) & 4 (from [0, 4]) i.e. 1⊕ 2⊕ 4 = 7



Input: A = 6, B = 2, C = 10
Output: 15
Explanation: Maximum XOR can be calculated by selecting 5 (from [0, 6]), 1 (from [0, 2]) & 10 (from [0, 10]) i.e. 5⊕ 1⊕ 10 = 15.

Naive Approach: Generate all possible triplets in the above ranges and calculate the maximum XOR possible.

Time Complexity: O(A*B*C)

Auxiliary Space: O(1)

Efficient Approach: 

To find the largest value of an XOR operation, the value of XOR should have every bit to be a set bit i.e In a 32-bit number, the goal is to get the most 1 set starting left to right. This can be done by following the below steps:

  1. Create a variable ans which will store the value of maximum xor possible and initialize it to zero.
  2. To create a 32-bit value that would be maximum xor possible, run a loop for each of its bits from most significant bit to least significant bit.
  3. Now for each bit:
    • Create a variable cur that will store the minimum number in which that bit is set.
    • Check each range A, B, C and try to find a number which has a set bit on that position. This can be done by comparing A, B & C with cur, i.e. if any number from A, B & C is greater than or equal to cur then it will contain a set bit in that position. If a set bit is found in any of the number from A, B, C then maximum xor will also contain set bit at that position.
    • Now, if a set bit is found, then add cur to ans to set that bit in it, and decrease the value of the number in which the set bit is found (from A, B, C) by cur to unset that bit as it cannot be used again.
    • Do the above steps for each bit, to calculate the final value of answer.
  4. After the above steps, print the required result.

Implementation: 

C++




// C++ Program to implement
// the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to calculate
// maximum triplet XOR form 3 ranges
int maximumTripletXOR(int A, int B, int C)
{
    // Initialize a variable
    // to store the answer
    int ans = 0;
    for (int i = 30; i >= 0; i--) {
 
        // create minimum number
        // that have a set bit
        // at ith position
        int cur = 1 << i;
 
        // 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
        if (A >= cur) {
 
            // Increment the answer
            ans += cur;
 
            // Subtract this value from A
            A -= cur;
        }
 
        // Check for B
        else if (B >= cur) {
 
            // Increment the answer
            ans += cur;
 
            // Subtract this value from B
            B -= cur;
        }
 
        // Check for C
        else if (C >= cur) {
 
            // Increment the answer
            ans += cur;
 
            // Subtract this value from C
            C -= cur;
        }
 
        // If any of the above conditions
        // is not satisfied then
        // there is no way to turn that bit ON.
    }
    return ans;
}
 
// Driver Code
int main()
{
    int A = 6;
    int B = 2;
    int C = 10;
    cout << maximumTripletXOR(A, B, C);
}


Java




// Java Program to implement
// the above approach
 
import java.util.*;
 
class GFG{
 
// Function to calculate
// maximum triplet XOR form 3 ranges
static int maximumTripletXOR(int A, int B, int C)
{
   
    // Initialize a variable
    // to store the answer
    int ans = 0;
    for (int i = 30; i >= 0; i--) {
 
        // create minimum number
        // that have a set bit
        // at ith position
        int cur = 1 << i;
 
        // 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
        if (A >= cur) {
 
            // Increment the answer
            ans += cur;
 
            // Subtract this value from A
            A -= cur;
        }
 
        // Check for B
        else if (B >= cur) {
 
            // Increment the answer
            ans += cur;
 
            // Subtract this value from B
            B -= cur;
        }
 
        // Check for C
        else if (C >= cur) {
 
            // Increment the answer
            ans += cur;
 
            // Subtract this value from C
            C -= cur;
        }
 
        // If any of the above conditions
        // is not satisfied then
        // there is no way to turn that bit ON.
    }
    return ans;
}
 
// Driver Code
public static void main(String[] args)
{
    int A = 6;
    int B = 2;
    int C = 10;
    System.out.print(maximumTripletXOR(A, B, C));
}
}
 
// This code is contributed by 29AjayKumar


Python3




# Python Program to implement
# the above approach
 
# Function to calculate
# maximum triplet XOR form 3 ranges
def maximumTripletXOR(A, B, C):
     
    # Initialize a variable
    # to store the answer
    ans = 0
     
    for i in range(30, -1,-1):
         
        # create minimum number
        # that have a set bit
        # at ith position
        cur = 1 << i
         
        # 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
        if (A >= cur):
             
            # Increment the answer
            ans += cur
             
            # Subtract this value from A
            A -= cur
        # Check for B
        elif (B >= cur):
             
            # Increment the answer
            ans += cur
             
            # Subtract this value from B
            B -= cur
         
        # Check for C
        elif (C >= cur):
            # Increment the answer
            ans += cur
             
            # Subtract this value from C
            C -= cur
         
        # If any of the above conditions
        # is not satisfied then
        # there is no way to turn that bit ON.
         
    return ans
     
# Driver Code
 
A = 6
B = 2
C = 10
print(maximumTripletXOR(A, B, C))
 
# This code is contributed by shivanisinghss2110


C#




// C# Program to implement
// the above approach
using System;
 
class GFG{
 
// Function to calculate
// maximum triplet XOR form 3 ranges
static int maximumTripletXOR(int A, int B, int C)
{
   
    // Initialize a variable
    // to store the answer
    int ans = 0;
    for (int i = 30; i >= 0; i--) {
 
        // create minimum number
        // that have a set bit
        // at ith position
        int cur = 1 << i;
 
        // 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
        if (A >= cur) {
 
            // Increment the answer
            ans += cur;
 
            // Subtract this value from A
            A -= cur;
        }
 
        // Check for B
        else if (B >= cur) {
 
            // Increment the answer
            ans += cur;
 
            // Subtract this value from B
            B -= cur;
        }
 
        // Check for C
        else if (C >= cur) {
 
            // Increment the answer
            ans += cur;
 
            // Subtract this value from C
            C -= cur;
        }
 
        // If any of the above conditions
        // is not satisfied then
        // there is no way to turn that bit ON.
    }
    return ans;
}
 
// Driver Code
public static void Main(String[] args)
{
    int A = 6;
    int B = 2;
    int C = 10;
    Console.Write(maximumTripletXOR(A, B, C));
}
}
 
// This code is contributed by shivanisinghss2110


Javascript




<script>
 
// JavaScript program to implement
// the above approach
 
// Function to calculate maximum
// triplet XOR form 3 ranges
function maximumTripletXOR(A, B, C)
{
     
    // Initialize a variable
    // to store the answer
    let ans = 0;
    for(let i = 30; i >= 0; i--)
    {
         
        // Create minimum number
        // that have a set bit
        // at ith position
        let cur = 1 << i;
 
        // 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
        if (A >= cur)
        {
             
            // Increment the answer
            ans += cur;
 
            // Subtract this value from A
            A -= cur;
        }
 
        // Check for B
        else if (B >= cur)
        {
             
            // Increment the answer
            ans += cur;
 
            // Subtract this value from B
            B -= cur;
        }
 
        // Check for C
        else if (C >= cur)
        {
             
            // Increment the answer
            ans += cur;
 
            // Subtract this value from C
            C -= cur;
        }
 
        // If any of the above conditions
        // is not satisfied then
        // there is no way to turn that bit ON.
    }
    return ans;
}
 
// Driver Code
let A = 6;
let B = 2;
let C = 10;
 
document.write(maximumTripletXOR(A, B, C));
 
// This code is contributed by Potta Lokesh
 
</script>


Output

15

Time Complexity: O(1)

Auxiliary Space: O(1)




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!