Mismo número de bits establecidos que N

Dado un entero positivo N, averigüe cuántos enteros positivos estrictamente menores que N tienen el mismo número de bits establecidos que N.

Ejemplos:  

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 

Acercarse:  

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

A continuación se muestra la implementación del enfoque anterior.  

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)
?>

Javascript

<script>
 
// Javascript program to find numbers less than N
// that have same Number Of Set Bits As N
 
// returns number of set bits in a number
function __builtin_popcount(n)
{
    var d, t = 0;
     
    while(n > 0)
    {
        d = n % 2;
        n = parseInt(n / 2);
        if(d == 1)
            t++;
    }
    return t;
}
 
function smallerNumsWithSameSetBits(n)
{       
    // __builtin_popcount function that count
    // set bits in n
    var temp = __builtin_popcount(n);
     
    // Iterate from n-1 to 1
    var count = 0;
    for (var 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
var n = 4;
document.write( smallerNumsWithSameSetBits(n));
 
 
</script>
Producción: 

2

 

Publicación traducida automáticamente

Artículo escrito por ajay0007 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *