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:
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.
- Calculate Bitwise OR of two integers from their given Bitwise AND and Bitwise XOR values
- Minimum possible Bitwise OR of all Bitwise AND of pairs generated from two given arrays
- Total pairs in an array such that the bitwise AND, bitwise OR and bitwise XOR of LSB is 1
- Maximize sum of squares of array elements possible by replacing pairs with their Bitwise AND and Bitwise OR
- 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]
- Sum of Bitwise AND of all pairs possible from two arrays
- Maximum Bitwise AND pair from given range
- Maximum Bitwise OR pair from a range
- Maximum Bitwise AND pair (X, Y) from given range such that X and Y can be same
- Leftover element after performing alternate Bitwise OR and Bitwise XOR operations on adjacent pairs
- Count ways to generate pairs having Bitwise XOR and Bitwise AND equal to X and Y respectively
- Count pairs with bitwise XOR exceeding bitwise AND from a given array
- Count pairs with equal Bitwise AND and Bitwise OR value
- Non-negative pairs with sum of Bitwise OR and Bitwise AND equal to N
- Maximum XOR value of maximum and second maximum element among all possible subarrays
- Find two numbers with given sum and maximum possible LCM
- Compute maximum of two integers in C/C++ using Bitwise Operators
- Russian Peasant (Multiply two numbers using bitwise operators)
- Sum of bitwise OR of all possible subsets of given set
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.