Same Number Of Set Bits As N

Given a positive integer N, find out how many positive integers strictly less than N have the same number of set bits as N.

Examples :

Input : 8
Output :3
Explanation: Binary representation of
8 : 1000, so number of set bits in 8 is 1.
So the integers less than 8 with same number 
of set bits are : 4, 2, 1

Input :1
Output :0

Input :4
Output :2


Approach:

1. Using __builtin_popcount() inbuilt function, count set bits in N and store into a 
temp variable 
2. Iterate from n-1 to 1 and also count set bits in i using __builtin_popcount() 
function
3. Now, compare temp with __builtin_popcount(i)
4. If both are equal then increment counter variable
5. Return counter

Below is the implementation of above approach.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to find numbers less than N
// that have same Number Of Set Bits As N
#include <iostream>
using namespace std;
  
int smallerNumsWithSameSetBits(int n)
{        
    // __builtin_popcount function that count
    // set bits in n
    int temp = __builtin_popcount(n);
      
    // Iterate from n-1 to 1
    int count = 0;
    for (int i = n - 1; i > 0; i--) {
          
        // check if the number of set bits 
        // equals to temp increment count        
        if (temp == __builtin_popcount(i))
            count++;
    }
    return count;
}
  
// Driver Code 
int main()
{
    int n = 4;
    cout << smallerNumsWithSameSetBits(n);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find numbers less than N
// that have same Number Of Set Bits As N
class GFG {
      
    // returns number of set bits in a number
    static int __builtin_popcount(int n)
    {
        int d, t = 0;
          
        while(n > 0)
        {
            d = n % 2;
            n = n / 2;
            if(d == 1)
                t++;
        }
        return t;
    }
      
    static int smallerNumsWithSameSetBits(int n)
    {     
        // __builtin_popcount function that count
        // set bits in n
        int temp = __builtin_popcount(n);
          
        // Iterate from n-1 to 1
        int count = 0;
        for (int i = n - 1; i > 0; i--) {
              
            // check if the number of set bits 
            // equals to temp increment count     
            if (temp == __builtin_popcount(i))
                count++;
        }
        return count;
    }
      
    // Driver Code 
    public static void main(String[] args)
    {
        int n = 4;
        System.out.println(
            smallerNumsWithSameSetBits(n));
    }
}
  
// This code is contributed by Arnab Kundu.

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to find numbers 
# less than N that have same
# Number Of Set Bits As N
def __builtin_popcount(n) :
    t = 0    
    while(n > 0) :
        d = n % 2
        n = int(n / 2)
        if(d == 1) :
            t = t + 1
    return t
  
def smallerNumsWithSameSetBits(n) :    
      
    # __builtin_popcount function 
    # that count set bits in n
    temp = __builtin_popcount(n)
      
    # Iterate from n-1 to 1
    count = 0
    for i in range(n-1,0,-1) :     
          
        # check if the number of 
        # set bits equals to temp
        # increment count     
        if (temp == __builtin_popcount(i)) :
            count = count + 1
    return count
  
# Driver Code 
n = 4
print (smallerNumsWithSameSetBits(n))
  
# This code is contributed by 
# Manish Shaw(manishshaw1)

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find numbers less than N
// that have same Number Of Set Bits As N
using System;
  
class GFG {
      
    // returns number of set bits in a number
    static int __builtin_popcount(int n)
    {
        int d, t = 0;
           
        while(n > 0)
        {
            d = n % 2;
            n = n / 2;
            if(d == 1)
                t++;
        }
        return t;
    }
      
    static int smallerNumsWithSameSetBits(int n)
    {     
        // __builtin_popcount function that count
        // set bits in n
        int temp = __builtin_popcount(n);
          
        // Iterate from n-1 to 1
        int count = 0;
        for (int i = n - 1; i > 0; i--) {
              
            // check if the number of set bits 
            // equals to temp increment count     
            if (temp == __builtin_popcount(i))
                count++;
        }
        return count;
    }
      
    // Driver Code 
    static public void Main(String []args)
    {
        int n = 4;
        Console.WriteLine(
            smallerNumsWithSameSetBits(n));
    }
}
  
// This code is contributed by Arnab Kundu.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to find numbers 
// less than N that have same
// Number Of Set Bits As N
function __builtin_popcount($n)
{
    $t = 0;     
    while($n > 0)
    {
        $d = $n % 2;
        $n = intval($n / 2);
        if($d == 1)
            $t++;
    }
    return $t;
}
function smallerNumsWithSameSetBits($n)
{     
    // __builtin_popcount function 
    // that count set bits in n
    $temp = __builtin_popcount($n);
      
    // Iterate from n-1 to 1
    $count = 0;
    for ($i = $n - 1; $i > 0; $i--)
    {
          
        // check if the number of 
        // set bits equals to temp
        // increment count     
        if ($temp == __builtin_popcount($i))
            $count++;
    }
    return $count;
}
  
// Driver Code 
$n = 4;
echo (smallerNumsWithSameSetBits($n));
  
// This code is contributed by 
// Manish Shaw(manishshaw1)
?>

chevron_right


Output:

2


My Personal Notes arrow_drop_up

Sometimes you never realize the value of a moment until it becomes a memory

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.



Improved By : andrew1234, manishshaw1



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.