📜  数组中的元素计数,其设置位是 K 的倍数

📅  最后修改于: 2021-10-26 06:57:20             🧑  作者: Mango

给定一个由N 个元素组成的数组arr[]和一个整数K ,任务是计算所有设置位数是K倍数的元素。
例子:

方法:

  1. 将数组中的数字一一遍历。
  2. 计算数组中每个数字的设置位。
  3. 检查 setbits 计数是否是 K 的倍数。

下面是上述方法的实现:

C++
// C++ implementation of above approach
 
#include 
using namespace std;
 
// Function to find the count of numbers
int find_count(vector arr, int k)
{
 
    int ans = 0;
    for (int i : arr) {
 
        // Get the set-bits count of each element
        int x = __builtin_popcount(i);
 
        // Check if the setbits count
        // is divisible by K
        if (x % k == 0)
 
            // Increment the count
            // of required numbers by 1
            ans += 1;
    }
 
    return ans;
}
 
// Driver code
int main()
{
    vector arr = { 12, 345, 2, 68, 7896 };
    int K = 2;
 
    cout << find_count(arr, K);
 
    return 0;
}


Java
// Java implementation of above approach
 
class GFG{
 
// Function to find the count of numbers
static int find_count(int []arr, int k)
{
 
    int ans = 0;
    for (int i : arr) {
 
        // Get the set-bits count of each element
        int x = Integer.bitCount(i);
 
        // Check if the setbits count
        // is divisible by K
        if (x % k == 0)
 
            // Increment the count
            // of required numbers by 1
            ans += 1;
    }
 
    return ans;
}
 
// Driver code
public static void main(String[] args)
{
    int []arr = { 12, 345, 2, 68, 7896 };
    int K = 2;
 
    System.out.print(find_count(arr, K));
 
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 implementation of above approach
 
# Function to count total set bits
def bitsoncount(x):
    return bin(x).count('1')
 
# Function to find the count of numbers
def find_count(arr, k) :
 
    ans = 0
    for i in arr:
 
        # Get the set-bits count of each element
        x = bitsoncount(i)
 
        # Check if the setbits count
        # is divisible by K
        if (x % k == 0) :
            # Increment the count
            # of required numbers by 1
            ans += 1
    return ans
 
# Driver code
arr = [ 12, 345, 2, 68, 7896 ]
K = 2
print(find_count(arr, K))
 
# This code is contributed by Sanjit_Prasad


C#
// C# implementation of above approach
using System;
 
class GFG{
 
// Function to find the count of numbers
static int find_count(int []arr, int k)
{
    int ans = 0;
    foreach(int i in arr)
    {
 
        // Get the set-bits count of each element
        int x = countSetBits(i);
 
        // Check if the setbits count
        // is divisible by K
        if (x % k == 0)
             
            // Increment the count
            // of required numbers by 1
            ans += 1;
    }
 
    return ans;
}
 
static int countSetBits(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 = { 12, 345, 2, 68, 7896 };
    int K = 2;
 
    Console.Write(find_count(arr, K));
}
}
// This code is contributed by sapnasingh4991


Javascript


输出:
3

时间复杂度: O(N * M) ,其中 N 是数组的大小,M 是数组中最大数字的位数。
辅助空间复杂度:O(1)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程