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:
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Maximum Bitwise AND pair from given range
- 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
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.