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 numbersint 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 codeint main(){ vector<int> 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 numbersstatic 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 codepublic 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 bitsdef 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 = 2print(find_count(arr, K)) # This code is contributed by Sanjit_Prasad |
C#
// C# implementation of above approachusing System; class GFG{ // Function to find the count of numbersstatic 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 codepublic 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 |
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)
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. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
In case you wish to attend live classes with industry experts, please refer Geeks Classes Live and Geeks Classes Live USA


