Count integers in an Array which are multiples their bits counts
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 : 4
Explanation :
There numbers which are multiple of their setbits count are { 1, 2, 4, 6 }.
Input : arr[] = {10, 20, 30, 40}
Output : 3
Explanation :
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;
int find_count(vector< int >& arr)
{
int ans = 0;
for ( int i : arr) {
int x = __builtin_popcount(i);
if (i % x == 0)
ans += 1;
}
return ans;
}
int main()
{
vector< int > arr
= { 1, 2, 3, 4, 5, 6 };
cout << find_count(arr);
return 0;
}
|
Java
class GFG{
static int find_count( int []arr)
{
int ans = 0 ;
for ( int i : arr) {
int x = Integer.bitCount(i);
if (i % x == 0 )
ans += 1 ;
}
return ans;
}
public static void main(String[] args)
{
int []arr
= { 1 , 2 , 3 , 4 , 5 , 6 };
System.out.print(find_count(arr));
}
}
|
Python3
def bitsoncount(x):
return bin (x).count( '1' )
def find_count(arr) :
ans = 0
for i in arr :
x = bitsoncount(i)
if (i % x = = 0 ):
ans + = 1
return ans
arr = [ 1 , 2 , 3 , 4 , 5 , 6 ]
print (find_count(arr))
|
C#
using System;
public class GFG{
static int find_count( int []arr)
{
int ans = 0;
foreach ( int i in arr) {
int x = bitCount(i);
if (i % x == 0)
ans += 1;
}
return ans;
}
static int bitCount( long x)
{
int setBits = 0;
while (x != 0) {
x = x & (x - 1);
setBits++;
}
return setBits;
}
public static void Main(String[] args)
{
int []arr
= { 1, 2, 3, 4, 5, 6 };
Console.Write(find_count(arr));
}
}
|
Javascript
<script>
function find_count(arr)
{
var ans = 0;
for ( var i=0;i<=arr.length;i++) {
var x = bitCount(i);
if (i % x == 0)
ans += 1;
}
return ans;
}
function bitCount( x)
{
var setBits = 0;
while (x != 0) {
x = x & (x - 1);
setBits++;
}
return setBits;
}
var arr = [ 1, 2, 3, 4, 5, 6 ];
document.write(find_count(arr));
</script>
|
Time complexity:- O(nlog(max(arr[])), where n is the size of the array,
Space complexity:- O(1)
Approach 2:
Another approach would be to precompute the set bits count of all integers up to a certain limit (e.g., the maximum value in the array), and store them in an array or a hash table. Then, we can loop through the array and look up the set bits count of each element in the table, and check if it divides the element. This approach can be faster if we have to process multiple arrays with the same set of integers.
Here’s an example implementation of the second approach:
C++
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 1000000;
int set_bits_count[MAX_N + 1];
void precompute_set_bits_count() {
for ( int i = 1; i <= MAX_N; i++) {
set_bits_count[i] = set_bits_count[i >> 1] + (i & 1);
}
}
int find_count(vector< int >& arr)
{
int ans = 0;
for ( int i : arr) {
int x = set_bits_count[i];
if (i % x == 0)
ans += 1;
}
return ans;
}
int main()
{
precompute_set_bits_count();
vector< int > arr
= { 1, 2, 3, 4, 5, 6 };
cout << find_count(arr);
return 0;
}
|
Java
import java.util.*;
public class Main {
static final int MAX_N = 1000000 ;
static int [] set_bits_count = new int [MAX_N + 1 ];
static void precompute_set_bits_count() {
for ( int i = 1 ; i <= MAX_N; i++) {
set_bits_count[i] = set_bits_count[i >> 1 ] + (i & 1 );
}
}
static int find_count(ArrayList<Integer> arr) {
int ans = 0 ;
for ( int i : arr) {
int x = set_bits_count[i];
if (i % x == 0 )
ans += 1 ;
}
return ans;
}
public static void main(String[] args) {
precompute_set_bits_count();
ArrayList<Integer> arr = new ArrayList<>(Arrays.asList( 1 , 2 , 3 , 4 , 5 , 6 ));
System.out.println(find_count(arr));
}
}
|
Python3
MAX_N = 1000000
set_bits_count = [ 0 ] * (MAX_N + 1 )
def precompute_set_bits_count():
for i in range ( 1 , MAX_N + 1 ):
set_bits_count[i] = set_bits_count[i >> 1 ] + (i & 1 )
def find_count(arr):
ans = 0
for i in arr:
x = set_bits_count[i]
if i % x = = 0 :
ans + = 1
return ans
if __name__ = = '__main__' :
precompute_set_bits_count()
arr = [ 1 , 2 , 3 , 4 , 5 , 6 ]
print (find_count(arr))
|
C#
using System;
using System.Collections.Generic;
public class MainClass
{
const int MAX_N = 1000000;
static int [] set_bits_count = new int [MAX_N + 1];
static void precompute_set_bits_count()
{
for ( int i = 1; i <= MAX_N; i++)
{
set_bits_count[i] = set_bits_count[i >> 1] + (i & 1);
}
}
static int find_count(List< int > arr)
{
int ans = 0;
foreach ( int i in arr)
{
int x = set_bits_count[i];
if (i % x == 0)
ans += 1;
}
return ans;
}
public static void Main()
{
precompute_set_bits_count();
List< int > arr = new List< int >() { 1, 2, 3, 4, 5, 6 };
Console.WriteLine(find_count(arr));
}
}
|
Javascript
const MAX_N = 1000000;
const set_bits_count = new Array(MAX_N + 1).fill(0);
function precompute_set_bits_count() {
for (let i = 1; i <= MAX_N; i++) {
set_bits_count[i] = set_bits_count[i >> 1] + (i & 1);
}
}
function find_count(arr) {
let ans = 0;
for (let i of arr) {
let x = set_bits_count[i];
if (i % x == 0) {
ans += 1;
}
}
return ans;
}
if (require.main === module) {
precompute_set_bits_count();
const arr = [1, 2, 3, 4, 5, 6];
console.log(find_count(arr));
}
|
Time complexity:- O(nlog(max(arr[])), where n is the size of the array,
Auxiliary Space:- O(N)
Last Updated :
24 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...