Maximum Bitwise OR pair from a range
Last Updated :
11 Nov, 2021
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 approach: Iterate from L to R and check the bitwise OR for every possible pair and print the maximum value in the end.
Below is the implementation of the above approach:
C++
#include <climits>
#include <iostream>
using namespace std;
int maxOR( int L, int R)
{
int maximum = INT_MIN;
for ( int i = L; i < R; i++)
for ( int j = i + 1; j <= R; j++)
maximum = max(maximum, (i | j));
return maximum;
}
int main()
{
int L = 4, R = 5;
cout << maxOR(L, R);
return 0;
}
|
Java
class GFG
{
static int maxOR( int L, int R)
{
int maximum = Integer.MIN_VALUE;
for ( int i = L; i < R; i++)
for ( int j = i + 1 ; j <= R; j++)
maximum = Math.max(maximum, (i | j));
return maximum;
}
public static void main(String []args)
{
int L = 4 , R = 5 ;
System.out.println(maxOR(L, R));
}
}
|
Python3
def maxOR(L, R):
maximum = - 10 * * 9
for i in range (L, R):
for j in range (i + 1 , R + 1 ):
maximum = max (maximum, (i | j))
return maximum
L = 4
R = 5
print (maxOR(L, R))
|
C#
using System;
class GFG
{
static int maxOR( int L, int R)
{
int maximum = int .MinValue;
for ( int i = L; i < R; i++)
for ( int j = i + 1; j <= R; j++)
maximum = Math.Max(maximum, (i | j));
return maximum;
}
public static void Main(String []args)
{
int L = 4, R = 5;
Console.WriteLine(maxOR(L, R));
}
}
|
Javascript
<script>
function maxOR(L, R)
{
let maximum = Number.MIN_VALUE;
for (let i = L; i < R; i++)
for (let j = i + 1; j <= R; j++)
maximum = Math.max(maximum, (i | j));
return maximum;
}
let L = 4, R = 5;
document.write(maxOR(L, R));
</script>
|
Time Complexity: O(n2)
Auxiliary Space: O(1)
Efficient approach: OR operation sets the ith bit if either of the operands has the ith bit set. Our aim is to maximize the number of set bits.
Now the task is to find the most significant bit B where L and R differ. Bth bit will be set in R and not set in L. The maximum OR that can be generated will have all the bits significant to bit B same as L and R and all the bits less significant than B will be set as they will be inclusive in the range. Bth bit will be 1 in the OR as 1 | 0 will be 1.
Now, consider the binary representations of both L and R, from the most significant bit (MSB) to least significant bit (LSB). Suppose the first x bits are the same for both L and R. Then all possible A and B have the first x bits the same as L and R, since even if a single bit among these x bits change, the value shall move outside range [L, R] which is not allowed.
Next, all the bits less significant than the difference bit B will be set, including B.
Consider the following example,
L = 001100010
R = 001100110
First six bits are same for L and R, so all numbers N, such that L? N < R holds, will have their first six bits same as L (or R) and also OR of any two numbers in the range will have same first six bits.
Now, ignoring the first x bits, we find a mismatch at the bit. At this point, A can be constructed such that the mismatched bit is not set for A and all bits less significant than the current bit is set. Similarly, B can be constructed such that mismatch bit is set for B and all subsequent bits are not set in B.
Their OR shall have all bits including mismatch bit set.
Applying this logic to our example, we have A = 001100011 and B = 001100100 and their OR is 001100111 which is maximum possible.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
const int MAX = 64;
int maxOR( int L, int R)
{
if (L == R) {
return L;
}
int ans = 0;
for ( int i = MAX - 1; i >= 0; i--) {
int p, lbit, rbit;
p = 1 << i;
lbit = (L >> i) & 1;
rbit = (R >> i) & 1;
if ((rbit == 1) && (lbit == 0)) {
ans += (p << 1) - 1;
break ;
}
if (rbit == 1) {
ans += p;
}
}
return ans;
}
int main()
{
int L = 4, R = 5;
cout << maxOR(L, R);
return 0;
}
|
Java
class GFG
{
static int MAX = 64 ;
static int maxOR( int L, int R)
{
if (L == R)
{
return L;
}
int ans = 0 ;
for ( int i = MAX - 1 ; i >= 0 ; i--)
{
int p, lbit, rbit;
p = 1 << i;
lbit = (L >> i) & 1 ;
rbit = (R >> i) & 1 ;
if ((rbit == 1 ) && (lbit == 0 ))
{
ans += (p << 1 ) - 1 ;
break ;
}
if (rbit == 1 )
{
ans += p;
}
}
return ans;
}
public static void main(String[] args)
{
int L = 4 , R = 5 ;
System.out.println(maxOR(L, R));
}
}
|
Python3
MAX = 64 ;
def maxOR(L, R) :
if (L = = R) :
return L;
ans = 0 ;
for i in range ( MAX - 1 , - 1 , - 1 ) :
p = 1 << i;
lbit = (L >> i) & 1 ;
rbit = (R >> i) & 1 ;
if ((rbit = = 1 ) and (lbit = = 0 )) :
ans + = (p << 1 ) - 1 ;
break ;
if (rbit = = 1 ) :
ans + = p;
return ans;
if __name__ = = "__main__" :
L = 4 ; R = 5 ;
print (maxOR(L, R));
|
C#
using System;
class GFG
{
static int MAX = 64;
static int maxOR( int L, int R)
{
if (L == R)
{
return L;
}
int ans = 0;
for ( int i = MAX - 1; i >= 0; i--)
{
int p, lbit, rbit;
p = 1 << i;
lbit = (L >> i) & 1;
rbit = (R >> i) & 1;
if ((rbit == 1) && (lbit == 0))
{
ans += (p << 1) - 1;
break ;
}
if (rbit == 1)
{
ans += p;
}
}
return ans;
}
public static void Main(String[] args)
{
int L = 4, R = 5;
Console.WriteLine(maxOR(L, R));
}
}
|
Javascript
<script>
let MAX = 64;
function maxOR(L,R)
{
if (L == R)
{
return L;
}
let ans = 0;
for (let i = MAX - 1; i >= 0; i--)
{
let p, lbit, rbit;
p = 1 << i;
lbit = (L >> i) & 1;
rbit = (R >> i) & 1;
if ((rbit == 1) && (lbit == 0))
{
ans += (p << 1) - 1;
break ;
}
if (rbit == 1)
{
ans += p;
}
}
return ans;
}
let L = 4, R = 5;
document.write(maxOR(L, R));
</script>
|
Time Complexity: O(log2(R)), where R is the upper limit of the range.
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...