# 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

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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++

 `// C++ implementation of the approach ` `#include ` `#include ` `using` `namespace` `std; ` ` `  `// Function to return the maximum bitwise OR ` `// possible among all the possible pairs ` `int` `maxOR(``int` `L, ``int` `R) ` `{ ` `    ``int` `maximum = INT_MIN; ` ` `  `    ``// Check for every possible pair ` `    ``for` `(``int` `i = L; i < R; i++) ` `        ``for` `(``int` `j = i + 1; j <= R; j++) ` ` `  `            ``// Maximum among all (i, j) pairs ` `            ``maximum = max(maximum, (i | j)); ` ` `  `    ``return` `maximum; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `L = 4, R = 5; ` ` `  `    ``cout << maxOR(L, R); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java implementation of the approach ` `class` `GFG ` `{ ` ` `  `// Function to return the maximum bitwise OR ` `// possible among all the possible pairs ` `static` `int` `maxOR(``int` `L, ``int` `R) ` `{ ` `    ``int` `maximum = Integer.MIN_VALUE; ` ` `  `    ``// Check for every possible pair ` `    ``for` `(``int` `i = L; i < R; i++) ` `        ``for` `(``int` `j = i + ``1``; j <= R; j++) ` ` `  `            ``// Maximum among all (i, j) pairs ` `            ``maximum = Math.max(maximum, (i | j)); ` ` `  `    ``return` `maximum; ` `} ` ` `  `// Driver code ` `public` `static` `void` `main(String []args)  ` `{ ` `    ``int` `L = ``4``, R = ``5``; ` ` `  `    ``System.out.println(maxOR(L, R)); ` `} ` `} ` ` `  `// This code is contributed by Rajput-Ji `

## Python3

 `# Python3 implementation of the approach ` ` `  `# Function to return the maximum bitwise OR ` `# possible among all the possible pairs ` `def` `maxOR(L, R): ` `    ``maximum ``=` `-``10``*``*``9` ` `  `    ``# Check for every possible pair ` `    ``for` `i ``in` `range``(L, R): ` `        ``for` `j ``in` `range``(i ``+` `1``, R ``+` `1``): ` ` `  `            ``# Maximum among all (i, j) pairs ` `            ``maximum ``=` `max``(maximum, (i | j)) ` ` `  `    ``return` `maximum ` ` `  `# Driver code ` `L ``=` `4` `R ``=` `5` ` `  `print``(maxOR(L, R)) ` ` `  `# This code is contributed by Mohit Kumar `

## C#

 `// C# implementation of the approach ` `using` `System; ` `     `  `class` `GFG ` `{ ` ` `  `// Function to return the maximum bitwise OR ` `// possible among all the possible pairs ` `static` `int` `maxOR(``int` `L, ``int` `R) ` `{ ` `    ``int` `maximum = ``int``.MinValue; ` ` `  `    ``// Check for every possible pair ` `    ``for` `(``int` `i = L; i < R; i++) ` `        ``for` `(``int` `j = i + 1; j <= R; j++) ` ` `  `            ``// Maximum among all (i, j) pairs ` `            ``maximum = Math.Max(maximum, (i | j)); ` ` `  `    ``return` `maximum; ` `} ` ` `  `// Driver code ` `public` `static` `void` `Main(String []args)  ` `{ ` `    ``int` `L = 4, R = 5; ` ` `  `    ``Console.WriteLine(maxOR(L, R)); ` `} ` `} ` ` `  `// This code is contributed by Rajput-Ji `

Output:

```5
```

Time Complexity: O(n2)

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++

 `// C++ implementation of the approach ` `#include ` `using` `namespace` `std; ` ` `  `const` `int` `MAX = 64; ` ` `  `// Function to return the maximum bitwise OR ` `// possible among all the possible pairs ` `int` `maxOR(``int` `L, ``int` `R) ` `{ ` ` `  `    ``// If there is only a single value ` `    ``// in the range [L, R] ` `    ``if` `(L == R) { ` `        ``return` `L; ` `    ``} ` ` `  `    ``int` `ans = 0; ` ` `  `    ``// Loop through each bit from MSB to LSB ` `    ``for` `(``int` `i = MAX - 1; i >= 0; i--) { ` `        ``int` `p, lbit, rbit; ` `        ``p = 1 << i; ` `        ``lbit = (L >> i) & 1; ``// bit of left limit ` `        ``rbit = (R >> i) & 1; ``// bit of right limit ` ` `  `        ``// MSBs where the bits differ, ` `        ``// all bits from that bit are set ` `        ``if` `((rbit == 1) && (lbit == 0)) { ` `            ``ans += (p << 1) - 1; ` `            ``break``; ` `        ``} ` ` `  `        ``// If MSBs are same, then ans ` `        ``// bit is same as that of ` `        ``// bit of right or left limit ` `        ``if` `(rbit == 1) { ` `            ``ans += p; ` `        ``} ` `    ``} ` `    ``return` `ans; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `L = 4, R = 5; ` ` `  `    ``cout << maxOR(L, R); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java implementation of the approach ` `class` `GFG  ` `{ ` `static` `int` `MAX = ``64``; ` ` `  `// Function to return the maximum bitwise OR ` `// possible among all the possible pairs ` `static` `int` `maxOR(``int` `L, ``int` `R) ` `{ ` ` `  `    ``// If there is only a single value ` `    ``// in the range [L, R] ` `    ``if` `(L == R)  ` `    ``{ ` `        ``return` `L; ` `    ``} ` ` `  `    ``int` `ans = ``0``; ` ` `  `    ``// Loop through each bit from MSB to LSB ` `    ``for` `(``int` `i = MAX - ``1``; i >= ``0``; i--) ` `    ``{ ` `        ``int` `p, lbit, rbit; ` `        ``p = ``1` `<< i; ` `        ``lbit = (L >> i) & ``1``; ``// bit of left limit ` `        ``rbit = (R >> i) & ``1``; ``// bit of right limit ` ` `  `        ``// MSBs where the bits differ, ` `        ``// all bits from that bit are set ` `        ``if` `((rbit == ``1``) && (lbit == ``0``))  ` `        ``{ ` `            ``ans += (p << ``1``) - ``1``; ` `            ``break``; ` `        ``} ` ` `  `        ``// If MSBs are same, then ans ` `        ``// bit is same as that of ` `        ``// bit of right or left limit ` `        ``if` `(rbit == ``1``)  ` `        ``{ ` `            ``ans += p; ` `        ``} ` `    ``} ` `    ``return` `ans; ` `} ` ` `  `// Driver code ` `public` `static` `void` `main(String[] args)  ` `{ ` `    ``int` `L = ``4``, R = ``5``; ` ` `  `    ``System.out.println(maxOR(L, R)); ` `} ` `} ` ` `  `// This code is contributed by Rajput-Ji `

## Python3

 `# Python3 implementation of the approach  ` ` `  `MAX` `=` `64``;  ` ` `  `# Function to return the maximum bitwise OR  ` `# possible among all the possible pairs  ` `def` `maxOR(L, R) :  ` ` `  `    ``# If there is only a single value  ` `    ``# in the range [L, R]  ` `    ``if` `(L ``=``=` `R) : ` `        ``return` `L;  ` ` `  `    ``ans ``=` `0``;  ` ` `  `    ``# Loop through each bit from MSB to LSB  ` `    ``for` `i ``in` `range``(``MAX` `-` `1``, ``-``1``, ``-``1``) : ` `        ``p ``=` `1` `<< i;  ` `        ``lbit ``=` `(L >> i) & ``1``; ``# bit of left limit  ` `        ``rbit ``=` `(R >> i) & ``1``; ``# bit of right limit  ` ` `  `        ``# MSBs where the bits differ,  ` `        ``# all bits from that bit are set  ` `        ``if` `((rbit ``=``=` `1``) ``and` `(lbit ``=``=` `0``)) : ` `            ``ans ``+``=` `(p << ``1``) ``-` `1``;  ` `            ``break``;  ` ` `  `        ``# If MSBs are same, then ans  ` `        ``# bit is same as that of  ` `        ``# bit of right or left limit  ` `        ``if` `(rbit ``=``=` `1``) : ` `            ``ans ``+``=` `p;  ` `  `  `    ``return` `ans;  ` ` `  `# Driver code  ` `if` `__name__ ``=``=` `"__main__"` `:  ` ` `  `    ``L ``=` `4``; R ``=` `5``;  ` ` `  `    ``print``(maxOR(L, R));  ` ` `  `    ``# This code is contributed by kanugargng `

## C#

 `// C# implementation of the above approach  ` `using` `System; ` `     `  `class` `GFG  ` `{ ` `static` `int` `MAX = 64; ` ` `  `// Function to return the maximum bitwise OR ` `// possible among all the possible pairs ` `static` `int` `maxOR(``int` `L, ``int` `R) ` `{ ` ` `  `    ``// If there is only a single value ` `    ``// in the range [L, R] ` `    ``if` `(L == R)  ` `    ``{ ` `        ``return` `L; ` `    ``} ` ` `  `    ``int` `ans = 0; ` ` `  `    ``// Loop through each bit from MSB to LSB ` `    ``for` `(``int` `i = MAX - 1; i >= 0; i--) ` `    ``{ ` `        ``int` `p, lbit, rbit; ` `        ``p = 1 << i; ` `        ``lbit = (L >> i) & 1; ``// bit of left limit ` `        ``rbit = (R >> i) & 1; ``// bit of right limit ` ` `  `        ``// MSBs where the bits differ, ` `        ``// all bits from that bit are set ` `        ``if` `((rbit == 1) && (lbit == 0))  ` `        ``{ ` `            ``ans += (p << 1) - 1; ` `            ``break``; ` `        ``} ` ` `  `        ``// If MSBs are same, then ans ` `        ``// bit is same as that of ` `        ``// bit of right or left limit ` `        ``if` `(rbit == 1)  ` `        ``{ ` `            ``ans += p; ` `        ``} ` `    ``} ` `    ``return` `ans; ` `} ` ` `  `// Driver code ` `public` `static` `void` `Main(String[] args)  ` `{ ` `    ``int` `L = 4, R = 5; ` ` `  `    ``Console.WriteLine(maxOR(L, R)); ` `} ` `} ` ` `  `// This code is contributed by PrinciRaj1992 `

Output:

```5
```

Time Complexity: O(log2(R)), where R is the upper limit of the range.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.