# Count of elements in an Array whose set bits are in a multiple of K

Given an array **arr[]** of **N** elements and an integer **K**, the task is to count all the elements whose number of set bits is a multiple of **K**.

**Examples:**

Input:arr[] = {1, 2, 3, 4, 5}, K = 2

Output:2

Explanation:

Two numbers whose setbits count is multiple of 2 are {3, 5}.

Input:arr[] = {10, 20, 30, 40}, K = 4

Output:1

Explanation:

There number whose setbits count is multiple of 4 is {30}.

**Approach:**

- Traverse the numbers in the array one by one.
- Count the set bits of every number in the array.
- Check if the setbits count is a multiple of K or not.

Below is the implementation of the above approach:

## C++

`// C++ implementation of above approach ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find the count of numbers ` `int` `find_count(vector<` `int` `> 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<` `int` `> arr = { 12, 345, 2, 68, 7896 }; ` ` ` `int` `K = 2; ` ` ` ` ` `cout << find_count(arr, K); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

**Output:**

3

** Time complexity: O(N * M)**, where N is the size of the array, and M is the bits count of the largest number in the array.

**Auxiliary Space complexity:**O(1)Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: **DSA Self Paced**. Become industry ready at a student-friendly price.

## Recommended Posts:

- Count pairs in an array such that both elements has equal set bits
- Count pairs of elements such that number of set bits in their OR is B[i]
- Count pairs of elements such that number of set bits in their AND is B[i]
- Count of elements such that difference between sum of left and right sub arrays is equal to a multiple of k
- Count of integers in an Array whose length is a multiple of K
- XOR of all elements of array with set bits equal to K
- Sort an array according to count of set bits
- Sort an array according to count of set bits | Set 2
- Find any one of the multiple repeating elements in read only array | Set 2
- Rearrange an array such that product of every two consecutive elements is a multiple of 4
- Find any one of the multiple repeating elements in read only array
- Sorting array elements with set bits equal to K
- Count all pairs of an array which differ in K bits
- Count of pairs in an Array with same number of set bits
- Program to count number of set bits in an (big) array
- Generate an Array such with elements maximized through swapping bits
- Count integers in an Array which are multiples their bits counts
- Check if bits of a number has count of consecutive set bits in increasing order
- Count of Array elements to be divided by 2 to make at least K elements equal
- Count of array elements which is smaller than both its adjacent elements

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.