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++
// 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; } |
Java
// 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. |
Python3
# 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) |
C#
// 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. |
PHP
<?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) ?> |
Output:
2
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.