Check if a number has same number of set and unset bits

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to check if a number
// has same number of set and unset bits
#include <bits/stdc++.h>
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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


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#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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"); 
      
    

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP 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 
function checkSame($n
    $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 
$n = 12; 
  
// function to check 
if (checkSame($n)) 
    echo "YES"
else
    echo "NO"
  
// This code is contributed
// by Sach_Code
?>

chevron_right


Output:

YES


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.