Given a number, the task is to find XOR of count of 0s and count of 1s in binary representation of a given number.
Input : 5 Output : 3 Binary representation : 101 Count of 0s = 1, Count of 1s = 2 1 XOR 2 = 3. Input : 7 Output : 3 Binary representation : 111 Count of 0s = 0 Count of 1s = 3 0 XOR 3 = 3.
The idea is simple, we traverse through all bits of a number, count 0s and 1s and finally return XOR of two counts.
One observation is, for a number of the form 2^x – 1, the output is always x. We can directly produce answer for this case by first checking n+1 is a power of two or not.
This article is contributed by Shivam Pradhan (anuj_charm). 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.
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Binary representation of a given number
- Maximum 0's between two immediate 1's in binary representation
- Find value of k-th bit in binary representation
- Next greater number than N with exactly one bit different in binary representation of N
- 1 to n bit numbers with no consecutive 1s in binary representation
- Binary representation of previous number
- 1 to n bit numbers with no consecutive 1s in binary representation.
- Maximum distance between two 1's in Binary representation of N
- Largest number with binary representation is m 1's and m-1 0's
- Check if all the set bits of the binary representation of N are at least K places away
- Find the n-th number whose binary representation is a palindrome
- Convert numbers into binary representation and add them without carry
- Check if binary representation of a number is palindrome
- Length of the Longest Consecutive 1s in Binary Representation
- Prime Number of Set Bits in Binary Representation | Set 1
Improved By : chitranayal