# Count of multiples in an Array before every element

Given an array arr of size N, the task is to count the number of indices j (j<i) such that a[i] divides a[j], for all valid indexes i.

Examples:

Input: arr[] = {8, 1, 28, 4, 2, 6, 7}
Output: 0, 1, 0, 2, 3, 0, 1
No of multiples for each element before itself –
N(8) = 0 ()
N(1) = 1 (8)
N(28) = 0 ()
N(4) = 2 (28, 8)
N(2) = 3 (4, 28, 8)
N(6) = 0 ()
N(7) = 1 (28)

Input: arr[] = {1, 1, 1, 1}
Output: 0, 1, 2, 3

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Naive Approach: Traverse through all valid indices j, in range [0, i-1], for each index i; and count the divisors for each indexes.

Time Complexity: O(N2)
Space Complexity: O(1)

Efficient Approach: This approach is to use map. Increment the count of factors in the map while traversing the array and lookup for that count in the map to find all valid j (< i) without traversing back.

Below is the implementation of the above approach.

## C++

 `// C++ program to count of multiples ` `// in an Array before every element ` ` `  `#include ` `using` `namespace` `std; ` ` `  `// Function to find all factors of N ` `// and keep their count in map ` `void` `add_factors(``int` `n, ` `                 ``unordered_map<``int``, ``int``>& mp) ` `{ ` `    ``// Traverse from 1 to sqrt(N) ` `    ``// if i divides N, ` `    ``// increment i and N/i in map ` `    ``for` `(``int` `i = 1; i <= ``int``(``sqrt``(n)); i++) { ` `        ``if` `(n % i == 0) { ` `            ``if` `(n / i == i) ` `                ``mp[i]++; ` `            ``else` `{ ` `                ``mp[i]++; ` `                ``mp[n / i]++; ` `            ``} ` `        ``} ` `    ``} ` `} ` ` `  `// Function to count of multiples ` `// in an Array before every element ` `void` `count_divisors(``int` `a[], ``int` `n) ` `{ ` ` `  `    ``// To store factors all of all numbers ` `    ``unordered_map<``int``, ``int``> mp; ` ` `  `    ``// Traverse for all possible i's ` `    ``for` `(``int` `i = 0; i < n; i++) { ` `        ``// Printing value of a[i] in map ` `        ``cout << mp[a[i]] << ``" "``; ` ` `  `        ``// Now updating the factors ` `        ``// of a[i] in the map ` `        ``add_factors(a[i], mp); ` `    ``} ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `arr[] = { 8, 1, 28, 4, 2, 6, 7 }; ` `    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr); ` ` `  `    ``// Function call ` `    ``count_divisors(arr, n); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java program to count of multiples ` `// in an Array before every element ` `import` `java.util.*; ` ` `  `class` `GFG{ ` `  `  `// Function to find all factors of N ` `// and keep their count in map ` `static` `void` `add_factors(``int` `n, ` `                 ``HashMap mp) ` `{ ` `    ``// Traverse from 1 to Math.sqrt(N) ` `    ``// if i divides N, ` `    ``// increment i and N/i in map ` `    ``for` `(``int` `i = ``1``; i <= (Math.sqrt(n)); i++) { ` `        ``if` `(n % i == ``0``) { ` `            ``if` `(n / i == i) { ` `                ``if``(mp.containsKey(i)) ` `                    ``mp.put(i, mp.get(i) + ``1``); ` `                ``else` `                    ``mp.put(i, ``1``); ` `            ``} ` `            ``else` `{ ` `                ``if``(mp.containsKey(i)) ` `                    ``mp.put(i, mp.get(i) + ``1``); ` `                ``else` `                    ``mp.put(i, ``1``); ` `                ``if``(mp.containsKey(n / i)) ` `                    ``mp.put(n / i, mp.get(n / i) + ``1``); ` `                ``else` `                    ``mp.put(n / i, ``1``); ` `            ``} ` `        ``} ` `    ``} ` `} ` `  `  `// Function to count of multiples ` `// in an Array before every element ` `static` `void` `count_divisors(``int` `a[], ``int` `n) ` `{ ` `  `  `    ``// To store factors all of all numbers ` `    ``HashMap mp = ``new` `HashMap(); ` `  `  `    ``// Traverse for all possible i's ` `    ``for` `(``int` `i = ``0``; i < n; i++) { ` `        ``// Printing value of a[i] in map ` `        ``System.out.print(mp.get(a[i]) == ``null` `? ``0` `+ ``" "` `: mp.get(a[i]) + ``" "``); ` `  `  `        ``// Now updating the factors ` `        ``// of a[i] in the map ` `        ``add_factors(a[i], mp); ` `    ``} ` `} ` `  `  `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` `    ``int` `arr[] = { ``8``, ``1``, ``28``, ``4``, ``2``, ``6``, ``7` `}; ` `    ``int` `n = arr.length; ` `  `  `    ``// Function call ` `    ``count_divisors(arr, n); ` `  `  `} ` `} ` ` `  `// This code is contributed by 29AjayKumar `

## Python3

 `# Python 3 program to count of multiples ` `# in an Array before every element ` `from` `collections ``import` `defaultdict ` `import` `math ` `  `  `# Function to find all factors of N ` `# and keep their count in map ` `def` `add_factors(n, mp): ` ` `  `    ``# Traverse from 1 to sqrt(N) ` `    ``# if i divides N, ` `    ``# increment i and N/i in map ` `    ``for` `i ``in` `range``(``1``, ``int``(math.sqrt(n)) ``+` `1``,): ` `        ``if` `(n ``%` `i ``=``=` `0``): ` `            ``if` `(n ``/``/` `i ``=``=` `i): ` `                ``mp[i] ``+``=` `1`  `            ``else` `: ` `                ``mp[i] ``+``=` `1`  `                ``mp[n ``/``/` `i] ``+``=` `1`  `  `  `# Function to count of multiples ` `# in an Array before every element ` `def` `count_divisors(a, n): ` `  `  `    ``# To store factors all of all numbers ` `    ``mp ``=` `defaultdict(``int``) ` `  `  `    ``# Traverse for all possible i's ` `    ``for` `i ``in` `range``(n) : ` `        ``# Printing value of a[i] in map ` `        ``print``(mp[a[i]], end``=``" "``) ` `  `  `        ``# Now updating the factors ` `        ``# of a[i] in the map ` `        ``add_factors(a[i], mp) ` `  `  `# Driver code ` `if` `__name__ ``=``=` `"__main__"``: ` `     `  `    ``arr ``=` `[ ``8``, ``1``, ``28``, ``4``, ``2``, ``6``, ``7` `] ` `    ``n ``=` `len``(arr) ` `  `  `    ``# Function call ` `    ``count_divisors(arr, n) ` `  `  `# This code is contributed by chitranayal `

## C#

 `// C# program to count of multiples ` `// in an Array before every element ` `using` `System; ` `using` `System.Collections.Generic; ` ` `  `class` `GFG{ ` `   `  `// Function to find all factors of N ` `// and keep their count in map ` `static` `void` `add_factors(``int` `n, ` `                 ``Dictionary<``int``,``int``> mp) ` `{ ` `    ``// Traverse from 1 to Math.Sqrt(N) ` `    ``// if i divides N, ` `    ``// increment i and N/i in map ` `    ``for` `(``int` `i = 1; i <= (Math.Sqrt(n)); i++) { ` `        ``if` `(n % i == 0) { ` `            ``if` `(n / i == i) { ` `                ``if``(mp.ContainsKey(i)) ` `                    ``mp[i] = mp[i] + 1; ` `                ``else` `                    ``mp.Add(i, 1); ` `            ``} ` `            ``else` `{ ` `                ``if``(mp.ContainsKey(i)) ` `                    ``mp[i] = mp[i] + 1; ` `                ``else` `                    ``mp.Add(i, 1); ` `                ``if``(mp.ContainsKey(n / i)) ` `                    ``mp[n / i] = mp[n / i] + 1; ` `                ``else` `                    ``mp.Add(n / i, 1); ` `            ``} ` `        ``} ` `    ``} ` `} ` `   `  `// Function to count of multiples ` `// in an Array before every element ` `static` `void` `count_divisors(``int` `[]a, ``int` `n) ` `{ ` `   `  `    ``// To store factors all of all numbers ` `    ``Dictionary<``int``,``int``> mp = ``new` `Dictionary<``int``,``int``>(); ` `   `  `    ``// Traverse for all possible i's ` `    ``for` `(``int` `i = 0; i < n; i++) { ` `        ``// Printing value of a[i] in map ` `        ``Console.Write(!mp.ContainsKey(a[i]) ? 0 + ``" "` `: mp[a[i]] + ``" "``); ` `   `  `        ``// Now updating the factors ` `        ``// of a[i] in the map ` `        ``add_factors(a[i], mp); ` `    ``} ` `} ` `   `  `// Driver code ` `public` `static` `void` `Main(String[] args) ` `{ ` `    ``int` `[]arr = { 8, 1, 28, 4, 2, 6, 7 }; ` `    ``int` `n = arr.Length; ` `   `  `    ``// Function call ` `    ``count_divisors(arr, n); ` `   `  `} ` `} ` ` `  `// This code is contributed by sapnasingh4991 `

Output:

```0 1 0 2 3 0 1
```

Time Complexity: O(N * sqrt(N))

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.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.