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:
- Maximum Bitwise AND pair from given range
- Maximum Bitwise OR pair from a range
- Find subsequences with maximum Bitwise AND and Bitwise OR
- Bitwise OR (or | ) of a range
- Queries for bitwise OR in the index range [L, R] of the given array
- Maximum subset with bitwise OR equal to k
- Total pairs in an array such that the bitwise AND, bitwise OR and bitwise XOR of LSB is 1
- 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
- Sum of all even numbers in range L and R
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.