📜  对数组中的整数进行计数,该整数是其位数的倍数

📅  最后修改于: 2021-05-17 16:39:47             🧑  作者: Mango

给定一个由N个元素组成的数组arr [] ,任务是对所有元素(其设置位计数的倍数)进行计数。

例子:

Input : arr[] = { 1, 2, 3, 4, 5, 6 }
Output : 4
Explanation :
There numbers which are multiple of their setbits count are { 1, 2, 4, 6 }.

Input : arr[] = {10, 20, 30, 40}
Output : 3
Explanation :
There numbers which are multiple of their setbits count are { 10, 20, 40 }

方法:逐个循环遍历每个数组元素。计算数组中每个数字的设置位。检查当前整数是否是其设置位数的倍数。如果“是”,则将计数器加1,否则跳过该整数。

下面是上述方法的实现:

C++
#include 
using namespace std;
  
// Function to find the count of numbers
// which are multiple of its set bits count
int find_count(vector& arr)
{
    // variable to store count
    int ans = 0;
  
    // iterate over elements of array
    for (int i : arr) {
  
        // Get the set-bits count of each element
        int x = __builtin_popcount(i);
  
        // Check if the setbits count
        // divides the integer i
        if (i % x == 0)
  
            // Increment the count
            // of required numbers by 1
            ans += 1;
    }
  
    return ans;
}
  
// Driver code
int main()
{
    vector arr
        = { 1, 2, 3, 4, 5, 6 };
  
    cout << find_count(arr);
  
    return 0;
}


Java
class GFG{
   
// Function to find the count of numbers
// which are multiple of its set bits count
static int find_count(int []arr)
{
    // variable to store count
    int ans = 0;
   
    // iterate over elements of array
    for (int i : arr) {
   
        // Get the set-bits count of each element
        int x = Integer.bitCount(i);
   
        // Check if the setbits count
        // divides the integer i
        if (i % x == 0)
   
            // Increment the count
            // of required numbers by 1
            ans += 1;
    }
   
    return ans;
}
   
// Driver code
public static void main(String[] args)
{
    int []arr
        = { 1, 2, 3, 4, 5, 6 };
   
    System.out.print(find_count(arr));
   
}
}
  
// This code contributed by Princi Singh


Python3
# Python3 implementation of above approach
  
# function to return set bits count
def bitsoncount(x):
    return bin(x).count('1')
  
# Function to find the count of numbers 
# which are multiple of its set bits count 
def find_count(arr) :
    # variable to store count 
    ans = 0 
  
    # iterate over elements of array 
    for i in arr :
  
        # Get the set-bits count of each element 
        x = bitsoncount(i)
  
        # Check if the setbits count 
        # divides the integer i 
        if (i % x == 0):
  
            # Increment the count 
            # of required numbers by 1 
            ans += 1
  
    return ans
  
# Driver code 
arr = [ 1, 2, 3, 4, 5, 6 ]
  
print(find_count(arr))
  
# This code is contributed by Sanjit_Prasad


C#
using System;
  
public class GFG{
    
// Function to find the count of numbers
// which are multiple of its set bits count
static int find_count(int []arr)
{
    // Variable to store count
    int ans = 0;
    
    // Iterate over elements of array
    foreach (int i in arr) {
    
        // Get the set-bits count of each element
        int x = bitCount(i);
    
        // Check if the setbits count
        // divides the integer i
        if (i % x == 0)
    
            // Increment the count
            // of required numbers by 1
            ans += 1;
    }
    
    return ans;
}
static int bitCount(long x)
{
    int setBits = 0;
    while (x != 0) {
        x = x & (x - 1);
        setBits++;
    }
    return setBits;
}
    
// Driver code
public static void Main(String[] args)
{
    int []arr
        = { 1, 2, 3, 4, 5, 6 };
    
    Console.Write(find_count(arr));
    
}
}
// This code contributed by Princi Singh


输出 :

4

时间复杂度: -O(nlog(max(arr [])),其中n是数组的大小,
空间复杂度: -O(1)

想要从精选的最佳视频中学习并解决问题,请查看有关从基础到高级C++的C++基础课程以及有关语言和STL的C++ STL课程。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”