Maximum count of elements divisible on the left for any element | Function to return the maximum count of required elements ; divisible [ i ] will store true if arr [ i ] is divisible by any element on its right ; To store the maximum required count ; For every element of the array ; If the current element is divisible by any element on its right ; Find the count of element on the left which are divisible by the current element ; If arr [ j ] is divisible then set divisible [ j ] to true ; Update the maximum required count ; Driver code
function findMax ( arr , n ) { var divisible = Array ( n ) . fill ( false ) ; var res = 0 ; for ( var i = n - 1 ; i > 0 ; i -- ) { if ( divisible [ i ] ) continue ; var cnt = 0 ; for ( var j = 0 ; j < i ; j ++ ) { if ( ( arr [ j ] % arr [ i ] ) == 0 ) { divisible [ j ] = true ; cnt ++ ; } } res = Math . max ( res , cnt ) ; } return res ; } var arr = [ 8 , 1 , 28 , 4 , 2 , 6 , 7 ] ; var n = arr . length ; document . write ( findMax ( arr , n ) ) ;