Skip to content
Related Articles
Check if a number has same number of set and unset bits
• Difficulty Level : Easy
• Last Updated : 23 Apr, 2021

Given a number N, the task is to check whether the count of the set and unset bits in the given number are same.
Examples:

```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:

## C++

 `// C++ program to check if a number``// has same number of set and unset bits``#include ``using` `namespace` `std;` `// Function to check if a number``// has same setbits and unset bits``bool` `checkSame(``int` `n)``{``    ``int` `set = 0, unset = 0;` `    ``// iterate for all bits of a number``    ``while` `(n) {` `        ``// if set``        ``if` `(n & 1)``            ``set++;``        ``// if unset``        ``else``            ``unset++;` `        ``// right shift number by 1``        ``n = n >> 1;``    ``}` `    ``// is number of set bits are``    ``// equal to unset bits``    ``if` `(set == unset)``        ``return` `true``;``    ``else``        ``return` `false``;``}` `// Driver Code``int` `main()``{``    ``int` `n = 12;` `    ``// function to check``    ``if` `(checkSame(n))``        ``cout << ``"YES"``;``    ``else``        ``cout << ``"NO"``;``    ``return` `0;``}`

## Java

 `// Java program to check if``// a number has same number``// of set and unset bits``import` `java.io.*;` `class` `GFG``{``    ` `// Function to check if a``// number has same setbits``// and unset bits``static` `boolean` `checkSame(``int` `n)``{``    ``int` `set = ``0``;``    ``int` `unset = ``0``;` `    ``// iterate for all``    ``// bits of a number``    ``while` `(n > ``0``)``    ``{` `        ``// if set``        ``if` `((n & ``1``) ==  ``1``)``            ``set++;``            ` `        ``// if unset``        ``else``            ``unset++;` `        ``// right shift``        ``// number by 1``        ``n = n >> ``1``;``    ``}` `    ``// is number of set bits``    ``// are equal to unset bits``    ``if` `(set == unset)``        ``return` `true``;``    ``else``        ``return` `false``;``}` `// Driver Code``public` `static` `void` `main (String[] args)``{``int` `n = ``12``;` `// function to check``if` `(checkSame(n))``    ``System.out.println (``"YES"``);``else``    ``System.out.println(``"NO"``);``    ` `}``}` `// This code is contributed``// by akt_mit`

## Python3

 `# Python program to check if a number``# has same number of set and unset bits` `# Function to check if a number``# has same setbits and unset bits``def` `checkSame(n):``    ``set``, unset ``=` `0``, ``0` `    ``# iterate for all bits of a number``    ``while``(n):` `        ``# if set``        ``if` `(n ``and` `1``):``            ``set` `+` `1``            ` `        ``# if unset``        ``else``:``            ``unset ``+``=` `1` `        ``# right shift number by 1``        ``n ``=` `n >> ``1``        ` `    ``# is number of set bits are``    ``# equal to unset bits``    ``if` `(``set` `=``=` `unset):``        ``return` `True``    ``else``:``        ``return` `False` `# Driver Code``if` `__name__ ``=``=` `'__main__'``:``    ``n ``=` `12` `    ``# function to check``    ``if` `(checkSame(n)):``        ``print``(``"YES"``)``    ``else``:``        ``print``(``"NO"``)` `# This code is contributed``# by 29AjayKumar`

## C#

 `// C# program to check if``// a number has same number``// of set and unset bits``using` `System;` `public` `class` `GFG{``        ` `// Function to check if a``// number has same setbits``// and unset bits``static` `bool` `checkSame(``int` `n)``{``    ``int` `set` `= 0;``    ``int` `unset = 0;` `    ``// iterate for all``    ``// bits of a number``    ``while` `(n > 0)``    ``{` `        ``// if set``        ``if` `((n & 1) == 1)``            ``set``++;``            ` `        ``// if unset``        ``else``            ``unset++;` `        ``// right shift``        ``// number by 1``        ``n = n >> 1;``    ``}` `    ``// is number of set bits``    ``// are equal to unset bits``    ``if` `(``set` `== unset)``        ``return` `true``;``    ``else``        ``return` `false``;``}` `// Driver Code``    ` `    ``static` `public` `void` `Main (){``        ``int` `n = 12;` `    ``// function to check``    ``if` `(checkSame(n))``        ``Console.WriteLine(``"YES"``);``    ``else``        ``Console.WriteLine(``"NO"``);``    ` `    ``}``}`

## PHP

 `> 1;``    ``}` `    ``// is number of set bits are``    ``// equal to unset bits``    ``if` `(``\$set` `== ``\$unset``)``        ``return` `true;``    ``else``        ``return` `false;``}` `// Driver Code``\$n` `= 12;` `// function to check``if` `(checkSame(``\$n``))``    ``echo` `"YES"``;``else``    ``echo` `"NO"``;` `// This code is contributed``// by Sach_Code``?>`

## Javascript

 ``
Output:
`YES`

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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with industry experts, please refer DSA Live Classes

My Personal Notes arrow_drop_up