Given a number N, the task is to check whether the count of the set and unset bits in the given number are same.
Input: 12 Output: Yes 1100 is the binary representation of 12 which has 2 set and 2 unset bits Input: 14 Output: No
Approach: Traverse in the binary representation of the given number, check if the leftmost bit is set or not using n & 1. If n & 1 returns 1, then the left most bit is set. Right, shift the number every time by 1 to check the next bit. Once the binary representation is traversed completely, check if the number of set bit and unset bits are same. If they are same, print “YES” else print “NO”.
Below is the implementation of the above approach:
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
- Count unset bits of a number
- Find the smallest number with n set and m unset bits
- Find the largest number with n set and m unset bits
- Check whether all the bits are unset in the given range or not
- Check whether all the bits are unset in the given range
- Check if bits of a number has count of consecutive set bits in increasing order
- Check whether the number has only first and last bits set | Set 2
- Check whether the number has only first and last bits set
- Check if all bits of a number are set
- Check if a number has two adjacent set bits
- Check if a number has bits in alternate pattern | Set 1
- Check if a number has bits in alternate pattern | Set-2 O(1) Approach
- Unset the last m bits
- Unset bits in the given range
- Count unset bits in a range
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.