Count of elements on the left which are divisible by current element | Set 2 | Utility function to print the elements of the array ; Function to increment the count for each factor of given val ; Function to generate and print the required array ; Find max element of array ; Create count array of maxi size ; For every element of the array ; Count [ A [ i ] ] denotes how many previous elements are there whose factor is the current element . ; Increment in count array for factors of A [ i ] ; Print the generated array ; Driver code
function printArr ( arr , n ) { for ( let i = 0 ; i < n ; i ++ ) document . write ( arr [ i ] + " " ) ; } function IncrementFactors ( count , val ) { for ( let i = 1 ; i * i <= val ; i ++ ) { if ( val % i == 0 ) { if ( i == parseInt ( val / i ) ) { count [ i ] ++ ; } else { count [ i ] ++ ; count [ parseInt ( val / i ) ] ++ ; } } } } function generateArr ( A , n ) { let B = new Array ( n ) ; let maxi = Math . max ( ... A ) ; let count = new Array ( maxi + 1 ) . fill ( 0 ) ; for ( let i = 0 ; i < n ; i ++ ) { B [ i ] = count [ A [ i ] ] ; IncrementFactors ( count , A [ i ] ) ; } printArr ( B , n ) ; } let arr = [ 8 , 1 , 28 , 4 , 2 , 6 , 7 ] ; let n = arr . length ; generateArr ( arr , n ) ;