Count of elements on the left which are divisible by current element - GeeksforGeeks

10 Mar, 2022
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] = 0Examples:  
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 elements on its left are divisible by it.Below is the implementation of the above approach:  
C++
Java
Python3
C#
Javascript
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std; // Utility function to print the// elements of the arrayvoid printArr(int arr[], int n){    for (int i = 0; i < n; i++)        cout << arr[i] << " ";} // Function to generate and print// the required arrayvoid 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 codeint main(){    int A[] = { 3, 5, 1 };    int n = sizeof(A) / sizeof(A[0]);     generateArr(A, n);     return 0;}
// Java implementation of above approachclass GFG{ // Utility function to print the// elements of the arraystatic 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 arraystatic 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 codepublic static void main(String args[]){    int A[] = { 3, 5, 1 };    int n = A.length;     generateArr(A, n);}} // This code is contributed by PrinciRaj1992
# Python3 implementation of the approach # Utility function to print the# elements of the arraydef printArr(arr, n):     for i in arr:        print(i, end = " ") # Function to generate and print# the required arraydef 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 codeA = [3, 5, 1]n = len(A) generateArr(A, n) # This code is contributed by Mohit Kumar
// C# implementation of above approachusing System;     class GFG{ // Utility function to print the// elements of the arraystatic void printArr(int []arr, int n){    for (int i = 0; i < n; i++)        Console.Write(arr[i] + " ");} // Function to generate and print// the required arraystatic 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 codepublic static void Main(String []args){    int []A = { 3, 5, 1 };    int n = A.Length;     generateArr(A, n);}} // This code is contributed by Rajput-Ji
<script>// Javascript implementation of the approach // Utility function to print the// elements of the arrayfunction printArr(arr, n){    for (let i = 0; i < n; i++)        document.write(arr[i] + " ");} // Function to generate and print// the required arrayfunction generateArr(A, n){    let B = new Array(n);     // For every element of the array    for (let i = 0; i < n; i++) {         // To store the count of elements        // on the left that the current        // element divides        let cnt = 0;        for (let j = 0; j < i; j++) {            if (A[j] % A[i] == 0)                cnt++;        }        B[i] = cnt;    }     // Print the generated array    printArr(B, n);} // Driver code    let A = [ 3, 5, 1 ];    let n = A.length;     generateArr(A, n); </script>
0 0 2
 
Time Complexity: O(n2)
Auxiliary Space: O(n)
mohit kumar 29
princiraj1992
Rajput-Ji
nidhi_biet
subham348
Number Divisibility
Arrays
Mathematical
Arrays
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Window Sliding Technique
Trapping Rain Water
Reversal algorithm for array rotation
Building Heap from Array
Program to find sum of elements in a given array
Program for Fibonacci numbers
Write a program to print all permutations of a given string
C++ Data Types
Set in C++ Standard Template Library (STL)
Coin Change | DP-7