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:
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:
- Create a variable ans which will store the value of maximum xor possible and initialize it to zero.
- 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.
- 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.
- After the above steps, print the required result.
Implementation:
C++
#include <bits/stdc++.h>
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);
}
|
Java
import java.util.*;
class GFG{
static 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;
}
public static void main(String[] args)
{
int A = 6 ;
int B = 2 ;
int C = 10 ;
System.out.print(maximumTripletXOR(A, B, C));
}
}
|
Python3
def maximumTripletXOR(A, B, C):
ans = 0
for i in range ( 30 , - 1 , - 1 ):
cur = 1 << i
if (A > = cur):
ans + = cur
A - = cur
elif (B > = cur):
ans + = cur
B - = cur
elif (C > = cur):
ans + = cur
C - = cur
return ans
A = 6
B = 2
C = 10
print (maximumTripletXOR(A, B, C))
|
C#
using System;
class GFG{
static 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;
}
public static void Main(String[] args)
{
int A = 6;
int B = 2;
int C = 10;
Console.Write(maximumTripletXOR(A, B, C));
}
}
|
Javascript
<script>
function maximumTripletXOR(A, B, C)
{
let ans = 0;
for (let i = 30; i >= 0; i--)
{
let 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;
}
let A = 6;
let B = 2;
let C = 10;
document.write(maximumTripletXOR(A, B, C));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
17 Aug, 2021
Like Article
Save Article