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 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.
- Count smaller values whose XOR with x is greater than x
- Closest (or Next) smaller and greater numbers with same number of set bits
- Count smaller elements on right side and greater elements on left side using Binary Index Tree
- Count numbers whose difference with N is equal to XOR with N
- Count numbers whose XOR with N is equal to OR with N
- Count numbers whose sum with x is equal to XOR with x
- Range Query on array whose each element is XOR of index value and previous element
- Find the node whose xor with x gives maximum value
- Find the node whose xor with x gives minimum value
- Find a value whose XOR with given number is maximum
- Length of longest subsequence whose XOR value is odd
- Check if a number from every row can be selected such that xor of the numbers is greater than zero
- Count pairs with Bitwise XOR greater than both the elements of the pair
- Count pairs having bitwise XOR greater than K from a given array
- Count all pairs of adjacent nodes whose XOR is an odd number
- Given a set, find XOR of the XOR's of all subsets.
- Find XOR of two number without using XOR operator
- Choose X such that (A xor X) + (B xor X) is minimized
- Count number of subsets having a particular XOR value
- Count no. of ordered subsets having a particular XOR value