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.
Input: L = 3, R = 7
In all the possible pairs, pair (7, 7) gives the maximum value for bitwise AND.
Input: L = 54, R = 55
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(N2)
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:
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.
- Total pairs in an array such that the bitwise AND, bitwise OR and bitwise XOR of LSB is 1
- Maximum Bitwise AND pair from given range
- Find subsequences with maximum Bitwise AND and Bitwise OR
- Minimum possible Bitwise OR of all Bitwise AND of pairs generated from two given arrays
- Count ways to generate pairs having Bitwise XOR and Bitwise AND equal to X and Y respectively
- Find a distinct pair (x, y) in given range such that x divides y
- Generate a pair of integers from a range [L, R] whose LCM also lies within the range
- Maximum possible Bitwise OR of the two numbers from the range [L, R]
- Minimum integer with at most K bits set such that their bitwise AND with N is maximum
- Queries for bitwise AND in the index range [L, R] of the given array
- Subsequence pair from given Array having all unique and all same elements respectively
- Maximum time such that absolute difference between hour and minute lies in given range
- Find pair with maximum GCD for integers in range 2 to N
- Find a pair (n,r) in an integer array such that value of nPr is maximum
- Find a pair (n,r) in an integer array such that value of nCr is maximum
- Compute (a*b)%c such that (a%c) * (b%c) can be beyond range
- Find smallest positive number Y such that Bitwise AND of X and Y is Zero
- Check if there is any pair in a given range with GCD is divisible by k
- Greatest number that can be formed from a pair in a given Array
- Check if quantities of 3 distinct colors can be converted to a single color by given merge-pair operations
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.
Improved By : Amal Kumar Choubey