Count the divisors or multiples present in the Array for each element
Given an array A[] with N integers, for each integer A[i] in the array, the task is to find the number of integers A[j] (j != i) in the array such that A[i] % A[j] = 0 or A[j] % A[i] = 0.
Examples:
Input: A = {2, 3, 4, 5, 6}
Output: 2 1 1 0 2
Explanation:
For i=0, the valid indices are 2 and 4 as 4%2 = 0 and 6%2 = 0.
For i=1, the only valid index is 4 as 6%3 = 0.
For i=2, the only valid index is 0 as 4%2 = 0.
For i=3, there are no valid indices.
For i=0, the valid indices are 0 and 1 as 6%2 = 0 and 6%3 = 0.
Input: A = {6, 6, 6, 6, 6}
Output: 4 4 4 4 4
Approach: The given problem can be solved by using the observation that the number of integers that satisfies the given condition can be categorized into two cases. Suppose the current integer is P and Q is an integer that satisfies the given conditions.
- Case 1 where Q is a multiple of P. Therefore, the count of integers in the given array that are divisible by P is the required answer. This case can be handled using a simple modification of Sieve of Eratosthenes which is discussed here.
- Case 2 where P is a multiple of Q. Therefore, the count of integers in the given array that Q divides P is the required answer. This case can be handled similarly using sieve as that of 1st Case.
So, the required answer for any integer is the sum of resulting integers of Case 1 and Case 2. In cases where P = Q, both Case 1 and Case 2 represents the same value and should be considered only once.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void countIndex( int A[], int N)
{
int MAX = *max_element(A, A + N);
vector< int > freq(MAX + 1, 0);
for ( int i = 0; i < N; i++)
freq[A[i]]++;
vector< int > res(MAX + 1, 0);
for ( int i = 1; i <= MAX; ++i) {
for ( int j = i; j <= MAX; j += i) {
if (i == j) {
res[i] += (freq[j] - 1);
}
else {
res[i] += freq[j];
res[j] += freq[i];
}
}
}
for ( int i = 0; i < N; i++) {
cout << res[A[i]] << " " ;
}
}
int main()
{
int A[] = { 2, 3, 4, 5, 6 };
int N = sizeof (A) / sizeof ( int );
countIndex(A, N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void countIndex( int []A, int N)
{
int MAX = Arrays.stream(A).max().getAsInt();
int []freq = new int [MAX + 1 ];
for ( int i = 0 ; i < N; i++)
freq[A[i]]++;
int []res = new int [MAX + 1 ];
for ( int i = 1 ; i <= MAX; ++i) {
for ( int j = i; j <= MAX; j += i) {
if (i == j) {
res[i] += (freq[j] - 1 );
}
else {
res[i] += freq[j];
res[j] += freq[i];
}
}
}
for ( int i = 0 ; i < N; i++) {
System.out.print(res[A[i]]+ " " );
}
}
public static void main(String[] args)
{
int []A = { 2 , 3 , 4 , 5 , 6 };
int N = A.length;
countIndex(A, N);
}
}
|
Python3
def countIndex(A, N):
MAX = max (A)
freq = [ 0 for i in range ( MAX + 1 )]
for i in range (N):
if freq[A[i]] > 0 :
freq[A[i]] + = 1
else :
freq[A[i]] = 1
res = [ 0 for i in range ( MAX + 1 )]
for i in range ( 1 , MAX + 1 , 1 ):
for j in range (i, MAX + 1 , i):
if (i = = j):
res[i] + = (freq[j] - 1 )
else :
res[i] + = freq[j]
res[j] + = freq[i]
for i in range (N):
print (res[A[i]],end = " " )
if __name__ = = '__main__' :
A = [ 2 , 3 , 4 , 5 , 6 ]
N = len (A)
countIndex(A, N)
|
C#
using System;
public class GFG {
static void countIndex( int [] A, int N)
{
int MAX = A[0];
for ( int i = 1; i < N; i++) {
if (A[i] > MAX) {
MAX = A[i];
}
}
int [] freq = new int [MAX + 1];
for ( int i = 0; i < N; i++)
freq[A[i]]++;
int [] res = new int [MAX + 1];
for ( int i = 1; i <= MAX; ++i) {
for ( int j = i; j <= MAX; j += i) {
if (i == j) {
res[i] += (freq[j] - 1);
}
else {
res[i] += freq[j];
res[j] += freq[i];
}
}
}
for ( int i = 0; i < N; i++) {
Console.Write(res[A[i]] + " " );
}
}
static public void Main()
{
int [] A = { 2, 3, 4, 5, 6 };
int N = A.Length;
countIndex(A, N);
}
}
|
Javascript
<script>
function max_element(A, N) {
let MAX = Number.MIN_VALUE;
for (let i = 0; i < A.length; i++) {
if (A[i] > MAX) {
MAX = A[i];
}
}
return MAX;
}
function countIndex(A, N) {
let MAX = max_element(A, A + N);
let freq = new Array(MAX + 1).fill(0);
for (let i = 0; i < N; i++)
freq[A[i]]++;
let res = new Array(MAX + 1).fill(0);
for (let i = 1; i <= MAX; ++i) {
for (let j = i; j <= MAX; j += i) {
if (i == j) {
res[i] += (freq[j] - 1);
}
else {
res[i] += freq[j];
res[j] += freq[i];
}
}
}
for (let i = 0; i < N; i++) {
document.write(res[A[i]] + " " );
}
}
let A = [2, 3, 4, 5, 6];
let N = A.length;
countIndex(A, N);
</script>
|
Time Complexity: O(N*log N)
Auxiliary Space: O(MAX) where MAX represents the maximum integer in the given array.
Last Updated :
01 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...