Given an array **A[]** of **N** integers, the task is to generate an array **B[]** such that **B[i]** contains the count of indices **j** in **A[]** such that **j < i** and **A[j] % A[i] = 0**

**Examples:**

Input:arr[] = {3, 5, 1}

Output:0 0 2

3 and 5 do not divide any element on

their left but 1 divides 3 and 5.

Input:arr[] = {8, 1, 28, 4, 2, 6, 7}

Output:0 1 0 2 3 0 1

**Approach:** Run a loop from the first element to the last element of the array and for the current element, run another loop for the elements on its left and check how many element on its left are divisible by it.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Utility function to print the ` `// elements of the array ` `void` `printArr(` `int` `arr[], ` `int` `n) ` `{ ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `cout << arr[i] << ` `" "` `; ` `} ` ` ` `// Function to generate and print ` `// the required array ` `void` `generateArr(` `int` `A[], ` `int` `n) ` `{ ` ` ` `int` `B[n]; ` ` ` ` ` `// For every element of the array ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` ` ` `// To store the count of elements ` ` ` `// on the left that the current ` ` ` `// element divides ` ` ` `int` `cnt = 0; ` ` ` `for` `(` `int` `j = 0; j < i; j++) { ` ` ` `if` `(A[j] % A[i] == 0) ` ` ` `cnt++; ` ` ` `} ` ` ` `B[i] = cnt; ` ` ` `} ` ` ` ` ` `// Print the generated array ` ` ` `printArr(B, n); ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `A[] = { 3, 5, 1 }; ` ` ` `int` `n = ` `sizeof` `(A) / ` `sizeof` `(A[0]); ` ` ` ` ` `generateArr(A, n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of above approach ` `class` `GFG ` `{ ` ` ` `// Utility function to print the ` `// elements of the array ` `static` `void` `printArr(` `int` `arr[], ` `int` `n) ` `{ ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) ` ` ` `System.out.print(arr[i] + ` `" "` `); ` `} ` ` ` `// Function to generate and print ` `// the required array ` `static` `void` `generateArr(` `int` `A[], ` `int` `n) ` `{ ` ` ` `int` `[]B = ` `new` `int` `[n]; ` ` ` ` ` `// For every element of the array ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) ` ` ` `{ ` ` ` ` ` `// To store the count of elements ` ` ` `// on the left that the current ` ` ` `// element divides ` ` ` `int` `cnt = ` `0` `; ` ` ` `for` `(` `int` `j = ` `0` `; j < i; j++) ` ` ` `{ ` ` ` `if` `(A[j] % A[i] == ` `0` `) ` ` ` `cnt++; ` ` ` `} ` ` ` `B[i] = cnt; ` ` ` `} ` ` ` ` ` `// Print the generated array ` ` ` `printArr(B, n); ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String args[]) ` `{ ` ` ` `int` `A[] = { ` `3` `, ` `5` `, ` `1` `}; ` ` ` `int` `n = A.length; ` ` ` ` ` `generateArr(A, n); ` `} ` `} ` ` ` `// This code is contributed by PrinciRaj1992 ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` ` ` `# Utility function to print the ` `# elements of the array ` `def` `printArr(arr, n): ` ` ` ` ` `for` `i ` `in` `arr: ` ` ` `print` `(i, end ` `=` `" "` `) ` ` ` `# Function to generate and print ` `# the required array ` `def` `generateArr(A, n): ` ` ` `B ` `=` `[` `0` `for` `i ` `in` `range` `(n)] ` ` ` ` ` `# For every element of the array ` ` ` `for` `i ` `in` `range` `(n): ` ` ` ` ` `# To store the count of elements ` ` ` `# on the left that the current ` ` ` `# element divides ` ` ` `cnt ` `=` `0` ` ` `for` `j ` `in` `range` `(i): ` ` ` `if` `(A[j] ` `%` `A[i] ` `=` `=` `0` `): ` ` ` `cnt ` `+` `=` `1` ` ` ` ` `B[i] ` `=` `cnt ` ` ` ` ` `# Print the generated array ` ` ` `printArr(B, n) ` ` ` `# Driver code ` `A ` `=` `[` `3` `, ` `5` `, ` `1` `] ` `n ` `=` `len` `(A) ` ` ` `generateArr(A, n) ` ` ` `# This code is contributed by Mohit Kumar ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of above approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Utility function to print the ` `// elements of the array ` `static` `void` `printArr(` `int` `[]arr, ` `int` `n) ` `{ ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `Console.Write(arr[i] + ` `" "` `); ` `} ` ` ` `// Function to generate and print ` `// the required array ` `static` `void` `generateArr(` `int` `[]A, ` `int` `n) ` `{ ` ` ` `int` `[]B = ` `new` `int` `[n]; ` ` ` ` ` `// For every element of the array ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `{ ` ` ` ` ` `// To store the count of elements ` ` ` `// on the left that the current ` ` ` `// element divides ` ` ` `int` `cnt = 0; ` ` ` `for` `(` `int` `j = 0; j < i; j++) ` ` ` `{ ` ` ` `if` `(A[j] % A[i] == 0) ` ` ` `cnt++; ` ` ` `} ` ` ` `B[i] = cnt; ` ` ` `} ` ` ` ` ` `// Print the generated array ` ` ` `printArr(B, n); ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main(String []args) ` `{ ` ` ` `int` `[]A = { 3, 5, 1 }; ` ` ` `int` `n = A.Length; ` ` ` ` ` `generateArr(A, n); ` `} ` `} ` ` ` `// This code is contributed by Rajput-Ji ` |

*chevron_right*

*filter_none*

**Output:**

0 0 2

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.

## Recommended Posts:

- Count of elements on the left which are divisible by current element | Set 2
- Largest element smaller than current element on left for every element in Array
- Maximum count of elements divisible on the left for any element
- Count of array elements which are greater than all elements on its left
- Count of Array elements greater than all elements on its left and at least K elements on its right
- Count of Array elements greater than all elements on its left and next K elements on its right
- Reduce the array by deleting elements which are greater than all elements to its left
- Replace elements with absolute difference of smallest element on left and largest element on right
- Find the difference of count of equal elements on the right and the left for each element
- Find permutation of n which is divisible by 3 but not divisible by 6
- Count the number of elements in an array which are divisible by k
- Count of pairs of Array elements which are divisible by K when concatenated
- Minimum elements to change so that for an index i all elements on the left are -ve and all elements on the right are +ve
- Find the element before which all the elements are smaller than it, and after which all are greater
- Count smaller elements on right side and greater elements on left side using Binary Index Tree
- Replace every element with the greatest element on its left side
- Replace every element with the smallest element on its left side
- Find Array formed by adding each element of given array with largest element in new array to its left
- Maximize the number of indices such that element is greater than element to its left
- Count the number of pairs (i, j) such that either arr[i] is divisible by arr[j] or arr[j] is divisible by arr[i]

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.