Given an integer N, the task is to find the largest number M, where (M < N), such that N(XOR)M is equal to N(OR)M i.e. (N ^ M) = (N | M).
Input: N = 5
5 ^ 4 = 1 and 5 | 4 = 5. Therefore, XOR and OR between them are not equal.
5 ^ 3 = 6 and 5 | 3 = 7. Therefore, XOR and OR between them are not equal.
5 ^ 2 = 7 and 5 | 2 = 7. Therefore, XOR and OR between them are equal.
Input: N = 14
To get the required number M, traverse all the bits of N from its Least Significant Bit (LSB) to Most Significant Bit (MSB). Two cases arise here:
- If the ith bit of N is 1 then:
- If the ith bit of M is set to 1, then N^M will not be equal to N|M as (1^1 = 0) and (1|1 = 1).
- If the ith bit is set of M to 0, then N^M will be equal to N|M as (1^0 = 1) and (1|0 = 1).
- So if the ith bit of N is 1, set the ith bit of M to 0.
- If the ith bit of N is 0 then:
- If the ith bit of M is set to 1, then N^M will be equal to N|M as (0^1 = 1) and (0|1 = 1).
- If we set the ith bit of M to 0, then N^M will be equal to N|M as (0^0 = 0) and (0|0 = 0).
- So, if the ith bit of M is set to either 0 or 1, N^M will always be equal to N|M.
- As the largest value of M which is less than N has to be found out, always set the ith bit of M to 1.
- N = 5
- 32-bit representation of 5 = 00000000000000000000000000000101
- LSB index of 5 = 31
- MSB index of 5 = 29
- Traversing from LSB to MSB i.e. from 31 to 29:
- For index 31, N = 1. So M should be set to 0.
- For index 30, N = 0. So M should be set to 1.
- For index 29, N = 1. So M should be set to 0.
- Thus the 32-bit representation of M is 00000000000000000000000000000010, which is equal to 2 in decimal representation.
Below is the implementation of the above approach:
Time Complexity: O(log2 N)
Auxiliary Space: 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.
- Count pairs with bitwise XOR exceeding bitwise AND from a given array
- Count ways to generate pairs having Bitwise XOR and Bitwise AND equal to X and Y respectively
- Construct a Matrix with no element exceeding X and sum of two adjacent elements not exceeding Y
- Total pairs in an array such that the bitwise AND, bitwise OR and bitwise XOR of LSB is 1
- Calculate Bitwise OR of two integers from their given Bitwise AND and Bitwise XOR values
- Split an Array to maximize subarrays having equal count of odd and even elements for a cost not exceeding K
- Count of all possible numbers not exceeding M having suffix N
- Smallest number greater than or equal to N having sum of digits not exceeding S
- Count pairs with equal Bitwise AND and Bitwise OR value
- Largest number M having bit count of N such that difference between their OR and XOR value is maximized
- Pair of integers having least GCD among all given pairs having GCD exceeding K
- Count even length subarrays having bitwise XOR equal to 0
- Count nodes having Bitwise XOR of all edges in their path from the root equal to K
- Maximize subsequences having array elements not exceeding length of the subsequence
- Maximize count of pairs (i, j) from two arrays having element from first array not exceeding that from second array
- Count number of triangles possible with length of sides not exceeding N
- Maximum possible sum of non-adjacent array elements not exceeding K
- Leftover element after performing alternate Bitwise OR and Bitwise XOR operations on adjacent pairs
- Maximize sum of squares of array elements possible by replacing pairs with their Bitwise AND and Bitwise OR
- Make A, B and C equal by adding total value N to them
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.