Maximum XOR value of a pair from a range
Last Updated :
17 Oct, 2022
Given a range [L, R], we need to find two integers in this range such that their XOR is maximum among all possible choices of two integers. More Formally,
given [L, R], find max (A ^ B) where L <= A, B
Examples :
Input : L = 8
R = 20
Output : 31
31 is XOR of 15 and 16.
Input : L = 1
R = 3
Output : 3
A simple solution is to generate all pairs, find their XOR values and finally return the maximum XOR value.
An efficient solution is to consider pattern of binary values from L to R. We can see that first bit from L to R either changes from 0 to 1 or it stays 1 i.e. if we take the XOR of any two numbers for maximum value their first bit will be fixed which will be same as first bit of XOR of L and R itself.
After observing the technique to get first bit, we can see that if we XOR L and R, the most significant bit of this XOR will tell us the maximum value we can achieve i.e. let XOR of L and R is 1xxx where x can be 0 or 1 then maximum XOR value we can get is 1111 because from L to R we have all possible combination of xxx and it is always possible to choose these bits in such a way from two numbers such that their XOR becomes all 1. It is explained below with some examples,
Examples 1:
L = 8 R = 20
L ^ R = (01000) ^ (10100) = (11100)
Now as L ^ R is of form (1xxxx) we
can get maximum XOR as (11111) by
choosing A and B as 15 and 16 (01111
and 10000)
Examples 2:
L = 16 R = 20
L ^ R = (10000) ^ (10100) = (00100)
Now as L ^ R is of form (1xx) we can
get maximum xor as (111) by choosing
A and B as 19 and 20 (10011 and 10100)
So the solution of this problem depends on the value of (L ^ R) only. We will calculate the L^R value first and then from most significant bit of this value, we will add all 1s to get the final result.
C++
// C/C++ program to get maximum xor value
// of two numbers in a range
#include <bits/stdc++.h>
using namespace std;
// method to get maximum xor value in range [L, R]
int maxXORInRange(int L, int R)
{
// get xor of limits
int LXR = L ^ R;
// loop to get msb position of L^R
int msbPos = 0;
while (LXR)
{
msbPos++;
LXR >>= 1;
}
// Simply return the required maximum value.
return (1 << msbPos) -1; // 2 ^ msbPos - 1
}
// Driver code to test above methods
int main()
{
int L = 8;
int R = 20;
cout << maxXORInRange(L, R) << endl;
return 0;
}
Java
// Java program to get maximum xor value
// of two numbers in a range
class Xor
{
// method to get maximum xor value in range [L, R]
static int maxXORInRange(int L, int R)
{
// get xor of limits
int LXR = L ^ R;
// loop to get msb position of L^R
int msbPos = 0;
while (LXR > 0)
{
msbPos++;
LXR >>= 1;
}
// construct result by adding 1,
// msbPos times
int maxXOR = 0;
int two = 1;
while (msbPos-- >0)
{
maxXOR += two;
two <<= 1;
}
return maxXOR;
}
// main function
public static void main (String[] args)
{
int L = 8;
int R = 20;
System.out.println(maxXORInRange(L, R));
}
}
Python3
# Python3 program to get maximum xor
# value of two numbers in a range
# Method to get maximum xor
# value in range [L, R]
def maxXORInRange(L, R):
# get xor of limits
LXR = L ^ R
# loop to get msb position of L^R
msbPos = 0
while(LXR):
msbPos += 1
LXR >>= 1
# construct result by adding 1,
# msbPos times
maxXOR, two = 0, 1
while (msbPos):
maxXOR += two
two <<= 1
msbPos -= 1
return maxXOR
# Driver code
L, R = 8, 20
print(maxXORInRange(L, R))
# This code is contributed by Anant Agarwal.
C#
// C# program to get maximum xor
// value of two numbers in a range
using System;
class Xor
{
// method to get maximum xor
// value in range [L, R]
static int maxXORInRange(int L, int R)
{
// get xor of limits
int LXR = L ^ R;
// loop to get msb position of L^R
int msbPos = 0;
while (LXR > 0)
{
msbPos++;
LXR >>= 1;
}
// construct result by
// adding 1, msbPos times
int maxXOR = 0;
int two = 1;
while (msbPos-- >0)
{
maxXOR += two;
two <<= 1;
}
return maxXOR;
}
// Driver code
public static void Main()
{
int L = 8;
int R = 20;
Console.WriteLine(maxXORInRange(L, R));
}
}
// This code is contributed by Anant Agarwal.
PHP
<?php
// PHP program to get maximum
// xor value of two numbers
// in a range
// method to get maximum xor
// value in range [L, R]
function maxXORInRange($L, $R)
{
// get xor of limits
$LXR = $L ^ $R;
// loop to get msb
// position of L^R
$msbPos = 0;
while ($LXR)
{
$msbPos++;
$LXR >>= 1;
}
// construct result by
// adding 1, msbPos times
$maxXOR = 0;
$two = 1;
while ($msbPos--)
{
$maxXOR += $two;
$two <<= 1;
}
return $maxXOR;
}
// Driver Code
$L = 8;
$R = 20;
echo maxXORInRange($L, $R), "\n";
// This code is contributed by aj_36
?>
JavaScript
<script>
// Javascript program to get maximum xor
// value of two numbers in a range
// method to get maximum xor
// value in range [L, R]
function maxXORInRange(L, R)
{
// get xor of limits
let LXR = L ^ R;
// loop to get msb position of L^R
let msbPos = 0;
while (LXR > 0)
{
msbPos++;
LXR >>= 1;
}
// construct result by
// adding 1, msbPos times
let maxXOR = 0;
let two = 1;
while (msbPos-- > 0)
{
maxXOR += two;
two <<= 1;
}
return maxXOR;
}
let L = 8;
let R = 20;
document.write(maxXORInRange(L, R));
</script>
Output :
31
Time Complexity: O(log(R))
Auxiliary Space: O(1)
Similar Reads
Maximum Bitwise OR pair from a range Given a range [L, R], the task is to find a pair (X, Y) such that L ? X, Y ? R and (X | Y) is maximum among all the possible pairs then print the bitwise OR of the found pair.Examples: Input: L = 4, R = 5 Output: 5 The only pair is (4, 5) and (4 | 5) = 5.Input: L = 14, R = 2500 Output: 4095 Naive ap
10 min read
Maximum OR value of a pair in an Array | Set 2 Given an array arr[] of N positive elements, the task is to find the maximum bitwise OR value of a pair from the given array.Examples: Input: arr[] = {3, 6, 8, 16} Output: 24 Explanation: The pair giving maximum OR value is (8, 16) 8|16 = 24Input: arr[] = {8, 7, 3, 12} Output: 15 Explanation: There
5 min read
Maximum OR value of a pair in an Array | Set 2 Given an array arr[] of N positive elements, the task is to find the maximum bitwise OR value of a pair from the given array.Examples: Input: arr[] = {3, 6, 8, 16} Output: 24 Explanation: The pair giving maximum OR value is (8, 16) 8|16 = 24Input: arr[] = {8, 7, 3, 12} Output: 15 Explanation: There
5 min read
Maximum OR value of a pair in an array Given an array arr[] of N positive elements. The task is to find the maximum bitwise OR value of a pair from the given array.Examples: Input: arr[] = {4, 8, 12, 16} Output: 28 (12, 16) is the pair with the maximum bitwise OR. 12 | 16 = 28 Input: arr[] = {4, 8, 16, 2} Output: 24 Approach: Iterate ove
4 min read
Maximum AND value of a pair in an array Given an array of N positive elements, the task is to find the maximum AND value generated by any pair of elements from the array. Examples: Input: arr1[] = {16, 12, 8, 4}Output: 8Explanation: 8 AND12 will give us the maximum value 8 Input: arr1[] = {4, 8, 16, 2}Output: 0 Recommended PracticeMaximum
12 min read
Value in a given range with maximum XOR Given positive integers N, L, and R, we have to find the maximum value of N ? X, where X ? [L, R].Examples: Input : N = 7 L = 2 R = 23 Output : 23 Explanation : When X = 16, we get 7 ? 16 = 23 which is the maximum value for all X ? [2, 23].Input : N = 10 L = 5 R = 12 Output : 15 Explanation : When X
10 min read