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