Given a range [L, R], the task is to find the maximum bitwise OR of some pair (a, b) from the given range.
Input: L = 10, R = 20
Input: L = 56, R = 77
Approach: First, convert both the given integers L and R to their binary representations. Now, If L has less number of bits than R then push zero to the MSB side of L to make the number of bits of both L and R to be equal.
Now from the MSB side compare the individual bits of both L and R. As R is greater than L, we will find the case when ith bit of R is 1 and ith bit of L is 0. So after ith bit, make all the bits of L to be 1. This ensures that the modifications done in the bits of L will not exceed R, so it will be between L and R only. And doing this also ensures maximum bitwise or.
Below is the implementation of the above approach:
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Maximum Bitwise AND pair from given range
- Maximum Bitwise AND pair (X, Y) from given range such that X and Y can be same
- Maximum Bitwise OR pair from a range
- Find subsequences with maximum Bitwise AND and Bitwise OR
- Number of ways to obtain each numbers in range [1, b+c] by adding any two numbers in range [a, b] and [b, c]
- Bitwise OR (or | ) of a range
- Queries for bitwise OR in the index range [L, R] of the given array
- Total pairs in an array such that the bitwise AND, bitwise OR and bitwise XOR of LSB is 1
- Maximum subset with bitwise OR equal to k
- Numbers whose bitwise OR and sum with N are equal
- Check whether bitwise OR of N numbers is Even or Odd
- Size of the smallest subset with maximum Bitwise OR
- Find N distinct numbers whose bitwise Or is equal to K
- Russian Peasant (Multiply two numbers using bitwise operators)
- Leftover element after performing alternate Bitwise OR and Bitwise XOR operations on adjacent pairs
- Value in a given range with maximum XOR
- Find the maximum range [L,R] whose sum is divisible by M
- Sum of all even numbers in range L and R
- Maximum distance between two 1s in a Binary Array in a given range
- Biggest integer which has maximum digit sum in range from 1 to n
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.