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