Queries for counts of multiples in an array
Given an array of positive integers and many queries for divisibility. In every query, we are given an integer k ( > 0), we need to count all elements in the array which are perfectly divisible by ‘k’.
Example:
Input:
2 4 9 15 21 20
k = 2
k = 3
k = 5
Output:
3
3
2
Explanation:
Multiples of '2' in array are:- {2, 4, 20}
Multiples of '3' in array are:- {9, 15, 21}
Multiples of '5' in array are:- {15, 20}
Simple Approach is to traverse over every value of ‘k’ in whole array and count total multiples by checking modulus of every element of array i.e., for every element of i (0 < i < n), check whether arr[i] % k == 0 or not. If it’s perfectly divisible of k, then increment count. Time complexity of this approach is O(n * k) which is not efficient for large number of queries of k.
Efficient approach is to use the concept of Sieve of Eratosthenes. Let’s define the maximum value in array[] is ‘Max’. Since multiples of all numbers in array[] will always be less than Max, therefore we will iterate up-to ‘Max’ only.
Now for every value(say ‘q’) iterate q, 2q, 3q, … t.k(tk <= MAX) because all these numbers are multiples of ‘q‘ .Meanwhile store the count of all these number for every value of q(1, 2, … MAX) in ans[] array. After that we can answer every query in O(1) time.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
int * ans = NULL;
void countSieve( int arr[], int n)
{
int MAX = *max_element(arr, arr + n);
int cnt[MAX + 1];
ans = new int [MAX + 1];
memset (cnt, 0, sizeof (cnt));
memset (ans, 0, (MAX + 1) * sizeof ( int ));
for ( int i = 0; i < n; ++i)
++cnt[arr[i]];
for ( int i = 1; i <= MAX; ++i)
for ( int j = i; j <= MAX; j += i)
ans[i] += cnt[j];
return ;
}
int countMultiples( int k)
{
return ans[k];
}
int main()
{
int arr[] = { 2, 4, 9, 15, 21, 20 };
int n = sizeof (arr) / sizeof (arr[0]);
countSieve(arr, n);
int k = 2;
cout << countMultiples(k) << "\n" ;
k = 3;
cout << countMultiples(k) << "\n" ;
k = 5;
cout << countMultiples(k) << "\n" ;
return 0;
}
|
Java
class CountMultiples {
static int ans[];
static void countSieve( int arr[], int n)
{
int MAX = arr[ 0 ];
for ( int i = 1 ; i < n; i++)
MAX = Math.max(arr[i], MAX);
int cnt[] = new int [MAX + 1 ];
ans = new int [MAX + 1 ];
for ( int i = 0 ; i < n; ++i)
++cnt[arr[i]];
for ( int i = 1 ; i <= MAX; ++i)
for ( int j = i; j <= MAX; j += i)
ans[i] += cnt[j];
return ;
}
static int countMultiples( int k)
{
return ans[k];
}
public static void main(String args[])
{
int arr[] = { 2 , 4 , 9 , 15 , 21 , 20 };
int n = 6 ;
countSieve(arr, n);
int k = 2 ;
System.out.println(countMultiples(k));
k = 3 ;
System.out.println(countMultiples(k));
k = 5 ;
System.out.println(countMultiples(k));
}
}
|
Python3
ans = []
def countSieve(arr, n):
MAX = max (arr)
global ans
ans = [ 0 ] * ( MAX + 1 )
cnt = [ 0 ] * ( MAX + 1 )
for i in range (n):
cnt[arr[i]] + = 1
for i in range ( 1 , MAX + 1 ):
for j in range (i, MAX + 1 , i):
ans[i] + = cnt[j]
def countMultiples(k):
return (ans[k])
if __name__ = = "__main__" :
arr = [ 2 , 4 , 9 , 15 , 21 , 20 ]
n = len (arr)
countSieve(arr, n)
k = 2
print (countMultiples( 2 ))
k = 3
print (countMultiples( 3 ))
k = 5
print (countMultiples( 5 ))
|
C#
using System;
class GFG {
static int [] ans;
static void countSieve( int [] arr, int n)
{
int MAX = arr[0];
for ( int i = 1; i < n; i++)
MAX = Math.Max(arr[i], MAX);
int [] cnt = new int [MAX + 1];
ans = new int [MAX + 1];
for ( int i = 0; i < n; ++i)
++cnt[arr[i]];
for ( int i = 1; i <= MAX; ++i)
for ( int j = i; j <= MAX; j += i)
ans[i] += cnt[j];
return ;
}
static int countMultiples( int k)
{
return ans[k];
}
public static void Main()
{
int [] arr = { 2, 4, 9, 15, 21, 20 };
int n = 6;
countSieve(arr, n);
int k = 2;
Console.WriteLine(countMultiples(k));
k = 3;
Console.WriteLine(countMultiples(k));
k = 5;
Console.WriteLine(countMultiples(k));
}
}
|
PHP
<?php
$ans ;
function countSieve( $arr , $n )
{
global $ans ;
$MAX = $arr [0];
for ( $i = 1; $i < $n ; $i ++)
$MAX = max( $arr [ $i ], $MAX );
$cnt = array_fill (0, $MAX + 1, 0);
$ans = array_fill (0, $MAX + 1, 0);
for ( $i = 0; $i < $n ; ++ $i )
++ $cnt [ $arr [ $i ]];
for ( $i = 1; $i <= $MAX ; ++ $i )
for ( $j = $i ; $j <= $MAX ; $j += $i )
$ans [ $i ] += $cnt [ $j ];
return ;
}
function countMultiples( $k )
{
global $ans ;
return $ans [ $k ];
}
$arr = array ( 2, 4, 9, 15, 21, 20);
$n = 6;
countSieve( $arr , $n );
$k = 2;
echo countMultiples( $k ) . "\n" ;
$k = 3;
echo countMultiples( $k ) . "\n" ;
$k = 5;
echo countMultiples( $k ) . "\n" ;
?>
|
Javascript
<script>
let ans = [];
function countSieve(arr, n)
{
let MAX = arr[0];
for (let i = 1; i < n; i++)
MAX = Math.max(arr[i], MAX);
let cnt = Array.from({length: MAX + 1}, (_, i) => 0);
ans = Array.from({length: MAX + 1}, (_, i) => 0);
for (let i = 0; i < n; ++i)
++cnt[arr[i]];
for (let i = 1; i <= MAX; ++i)
for (let j = i; j <= MAX; j += i)
ans[i] += cnt[j];
return ;
}
function countMultiples(k)
{
return ans[k];
}
let arr = [ 2, 4, 9, 15, 21, 20 ];
let n = 6;
countSieve(arr, n);
let k = 2;
document.write(countMultiples(k) + "<br/>" );
k = 3;
document.write(countMultiples(k) + "<br/>" );
k = 5;
document.write(countMultiples(k) + "<br/>" );
</script>
|
Time complexity: O(M*log(M)) where M is the maximum value in array elements.
Auxiliary space: O(MAX)
Last Updated :
19 Sep, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...