Given a non-negative integer N, the task is to find count of non-negative integers
less than or equal to N whose bitwise OR and sum with N are equal.
Input : N = 3 Output : 1 0 is the only number in [0, 3] that satisfies given property. (0 + 3) = (0 | 3) Input : 10 Output : 4 (0 + 10) = (0 | 10) (Both are 10) (1 + 10) = (1 | 10) (Both are 11) (4 + 10) = (4 | 10) (Both are 14) (5 + 10) = (5 | 10) (Both are 15)
A simple solution is to traverse all numbers from 0 to N and do bitwise OR and SUM with N, if both are equal increment counter.
Time complexity = O(N).
An efficient solution is to follow following steps.
1. Find count of zero bit in N.
2. Return pow(2,count).
The idea is based on the fact that bitwise OR and sum of a number x with N are equal, if and only if
bitwise AND of x with N will is 0
Let, N=10 =10102. Bitwise AND of a number with N will be 0, if number contains zero bit with all respective set bit(s) of N and either zero bit or set bit with all respective zero bit(s) of N (because, 0&0=1&0=0). e.g. bit : 1 0 1 0 position: 4 3 2 1 Bitwise AND of any number with N will be 0, if the number has following bit pattern 1st position can be either 0 or 1 (2 ways) 2nd position can be 1 (1 way) 3rd position can be either 0 or 1 (2 ways) 4th position can be 1 (1 way) Total count = 2*1*2*1 = 22 = 4.
Total time complexity of above solution will be O(log2(N)).
This article is contributed by Viranjan Kumar Akela. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
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.
- Total pairs in an array such that the bitwise AND, bitwise OR and bitwise XOR of LSB is 1
- Count ways to generate pairs having Bitwise XOR and Bitwise AND equal to X and Y respectively
- Find N distinct numbers whose bitwise Or is equal to K
- 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
- Leftover element after performing alternate Bitwise OR and Bitwise XOR operations on adjacent pairs
- Minimum possible Bitwise OR of all Bitwise AND of pairs generated from two given arrays
- Count pairs with bitwise XOR exceeding bitwise AND from a given array
- Count of pairs from Array with sum equal to twice their bitwise AND
- Count numbers whose sum with x is equal to XOR with x
- Count of pairs whose bitwise AND is a power of 2
- Maximum sum of Bitwise XOR of all elements of two equal length subsets
- Rearrange array elements such that Bitwise AND of first N - 1 elements is equal to last element
- Minimize bits to be flipped in X and Y such that their Bitwise OR is equal to Z
- Count of pairs having bit size at most X and Bitwise OR equal to X
- Largest possible value of M not exceeding N having equal Bitwise OR and XOR between them
- Count numbers whose difference with N is equal to XOR with N
- Count numbers whose XOR with N is equal to OR with N
- Maximum subset with bitwise OR equal to k
- Largest set with bitwise OR equal to n
Improved By : jit_t