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:
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.
- Check if bits of a number has count of consecutive set bits in increasing order
- Count pairs (A, B) such that A has X and B has Y number of set bits and A+B = C
- 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 whether the number has only first and last bits set | Set 2
- Check if all bits can be made same by flipping two consecutive bits
- Count unset bits of a number
- Unset least significant K bits of a given number
- Unset bits in the given range
- Unset the last m bits
- Count unset bits in a range
- Python | Count unset bits in a range
- Count total unset bits in all the numbers from 1 to N
- Check whether the number has only first and last bits set
- Check if a number has bits in alternate pattern | Set 1
- Check if a number has two adjacent set bits
- Check if a number has bits in alternate pattern | Set-2 O(1) Approach
- Check whether the bit at given position is set or unset
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.