Given a **range [L, R]**, the task is to find a pair (X, Y), **not necessarily distinct**. Find the maximum possible value of the bitwise AND of the chosen integers.

**Examples:**

Input:L = 3, R = 7

Output:7

Explanation:

In all the possible pairs, pair (7, 7) gives the maximum value for bitwise AND.

Input:L = 54, R = 55

Output:55

Explanation:

In all the possible pairs, pair (55, 55) gives the maximum value for bitwise AND.

**Naive Approach: ** To solve the problem mentioned above the naive method is to iterate from L to R and check the bitwise AND for every possible pair and print the maximum value in the end.

**Time Complexity:** O(N^{2})

**Efficient Approach:**

To optimize the above method we have to observe that here we have to integers L and R and we have to select two integers from the interval [L, R] so that their bitwise AND should be maximum. Bitwise AND of any two numbers between L and R will be **always less than or equal to R only.** So if we have to select two integers from the interval, we can choose the integers to be R and that’s the only way to maximize the bitwise AND.

Below is the implementation of above approach:

## C

`// C implementation to find the ` `// Maximum Bitwise AND pair (X, Y) ` `// from given range such that ` `// X and Y can be same ` ` ` `#include <stdio.h> ` ` ` `// Function to return the ` `// maximum bitwise AND ` `int` `maximumAND(` `int` `L, ` `int` `R) ` `{ ` ` ` `return` `R; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `l = 3; ` ` ` `int` `r = 7; ` ` ` ` ` `printf` `(` `"%d"` `, maximumAND(l, r)); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## C++

`// C++ implementation to find the ` `// Maximum Bitwise AND pair (X, Y) ` `// from given range such that ` `// X and Y can be same ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the ` `// maximum bitwise AND ` `int` `maximumAND(` `int` `L, ` `int` `R) ` `{ ` ` ` `return` `R; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `l = 3; ` ` ` `int` `r = 7; ` ` ` ` ` `cout << maximumAND(l, r); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation to find the ` `// Maximum Bitwise AND pair (X, Y) ` `// from given range such that ` `// X and Y can be same ` ` ` `class` `GFG { ` ` ` ` ` `// Function to return the ` ` ` `// maximum bitwise AND ` ` ` `static` `int` `maximumAND(` `int` `L, ` `int` `R) ` ` ` `{ ` ` ` `return` `R; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `int` `l = ` `3` `; ` ` ` `int` `r = ` `7` `; ` ` ` `System.out.print(maximumAND(l, r)); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation to find the ` `# Maximum Bitwise AND pair (X, Y) ` `# from given range such that ` `# X and Y can be same ` ` ` `# Function to return the ` `# maximum bitwise AND ` `def` `maximumAND(L, R): ` ` ` `return` `R ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `l ` `=` `3` ` ` `r ` `=` `7` ` ` `print` `(maximumAND(l, r)) ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation to find the ` `// maximum Bitwise AND pair (X, Y) ` `// from given range such that ` `// X and Y can be same ` `using` `System; ` ` ` `class` `GFG{ ` ` ` `// Function to return the ` `// maximum bitwise AND ` `static` `int` `maximumAND(` `int` `L, ` `int` `R) ` `{ ` ` ` `return` `R; ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `int` `l = 3; ` ` ` `int` `r = 7; ` ` ` ` ` `Console.Write(maximumAND(l, r)); ` `} ` `} ` ` ` `// This code is contributed by amal kumar choubey ` |

*chevron_right*

*filter_none*

**Output:**

7

**Time Complexity:** O(1)

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.

## Recommended Posts:

- Maximum Bitwise OR pair from a range
- Maximum Bitwise AND pair from given range
- Maximum possible Bitwise OR of the two numbers from the range [L, R]
- Maximum XOR value of a pair from a range
- Find pair with maximum GCD for integers in range 2 to N
- Find subsequences with maximum Bitwise AND and Bitwise OR
- Bitwise and (or &) of a range
- Bitwise OR (or | ) of a range
- Queries for bitwise OR in the index range [L, R] of the given array
- Queries for bitwise AND in the index range [L, R] of the given array
- Check if there is any pair in a given range with GCD is divisible by k
- Find a distinct pair (x, y) in given range such that x divides y
- Total pairs in an array such that the bitwise AND, bitwise OR and bitwise XOR of LSB is 1
- Maximum bitwise OR value of subsequence of length K
- Maximum steps to transform 0 to X with bitwise AND
- Maximum Bitwise AND value of subsequence of length K
- Maximum subset with bitwise OR equal to k
- Minimum integer with at most K bits set such that their bitwise AND with N is maximum
- Maximum value of Bitwise AND from root to leaf in a Binary tree
- Maximum AND value of a pair in an array

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.