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:
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Count unset bits of a number
- Find the largest number with n set and m unset bits
- Find the smallest 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 if all bits of a number are set
- Check whether the number has only first and last bits 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
- Count total unset bits in all the numbers from 1 to N
- Python | Count unset bits in a range
- Find the largest number smaller than integer N with maximum number of set bits
- Maximize a given unsigned number number by swapping bits at it's extreme positions.
- Maximum number of contiguous array elements with same number of set bits
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.