Given an array **arr[]** of **N** elements, the task is to count all the elements which are a multiple of their set bits count.

**Examples:**

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

**Approach:** Loop through each array elements one by one. Count the set bits of every number in the array. Check if the current integer is a multiple of its set bits count or not. If ‘yes’ then increment the counter by 1, else skip that integer.

Below is the implementation of above approach:

## C++

`#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find the count of numbers ` `// which are multiple of its set bits count ` `int` `find_count(vector<` `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 = __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<` `int` `> arr ` ` ` `= { 1, 2, 3, 4, 5, 6 }; ` ` ` ` ` `cout << find_count(arr); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

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

*chevron_right*

*filter_none*

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

*chevron_right*

*filter_none*

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

*chevron_right*

*filter_none*

**Output :**

4

**Time complexity:-** O(nlog(max(arr[])), where n is the size of the array,

**Space complexity:-** O(1)

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.

## Recommended Posts:

- Queries for counts of multiples in an array
- Generate original array from an array that store the counts of greater elements on right
- Rearrange all elements of array which are multiples of x in increasing order
- Rearrange all elements of array which are multiples of x in decreasing order
- Count pairs from 1 to N such that their Sum is divisible by their XOR
- Queries for counts of array elements with values in given range
- Counts Path in an Array
- Minimum operations to make counts of remainders same in an array
- Count pairs of elements such that number of set bits in their AND is B[i]
- Count pairs of elements such that number of set bits in their OR is B[i]
- Queries to count integers in a range [L, R] such that their digit sum is prime and divisible by K
- Count all elements in the array which appears at least K times after their first occurrence
- Count of multiples in an Array before every element
- Check if bits of a number has count of consecutive set bits in increasing order
- Sort integers in array according to their distance from the element K
- Count all pairs of an array which differ in K bits
- XOR counts of 0s and 1s in binary representation
- Counts paths from a point to reach Origin
- Replace the odd positioned elements with their cubes and even positioned elements with their squares
- Minimum product of k integers in an array of positive Integers

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.