Given a positive integer n, count numbers x such that 0 < x <n and x^n > n where ^ is bitwise XOR operation.
Input : n = 12 Output : 3 Numbers are 1, 2 and 3 1^12 > 12, 2^12 > 12 and 3^12 > 12 Input : n = 11 Output : 4 Numbers are 4, 5, 6 and 7
A number may x produce a greater XOR value if x has a set bit at a position where n has a 0 bit. So we traverse bits of n, and one by one consider all 0 bits. For every set bit at position k (Considering k = 0 for rightmost bit, k = 1 for second rightmost bit, ..), we add 2 2k to result. For a bit at k-th position, there are 2k numbers with set bit 1.
Below is the implementation of the above idea.
Time complexity : O(Log n)
This article is contributed by Smarak Chopdar. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Count smaller values whose XOR with x is greater than x
- Closest (or Next) smaller and greater numbers with same number of set bits
- Check if a number from every row can be selected such that xor of the numbers is greater than zero
- First element greater than or equal to X in prefix sum of N numbers using Binary Lifting
- Count of numbers having only 1 set bit in the range [0, n]
- Count numbers whose sum with x is equal to XOR with x
- Count numbers whose XOR with N is equal to OR with N
- Count total set bits in all numbers from 1 to n
- Count total set bits in all numbers from 1 to n | Set 2
- Count numbers whose difference with N is equal to XOR with N
- Count ordered pairs of positive numbers such that their sum is S and XOR is K
- Count of numbers which can be made power of 2 by given operation
- Python map function | Count total set bits in all numbers from 1 to n
- Round to next smaller multiple of 8
- Previous smaller integer having one less number of set bits