Size of the largest divisible subset in an Array | Javascript implementation of the above approach ; Function to find the size of the largest divisible subarray ; Mark all elements of the array ; Traverse reverse ; For all multiples of i ; Return the required answer ; Driver code ; Function call
let N = 1000005 function maximum_set ( a , n ) { let dp = new Array ( N ) . fill ( 0 ) ; for ( let i = 0 ; i < n ; i ++ ) dp [ a [ i ] ] = 1 ; let ans = 1 ; for ( let i = N - 1 ; i >= 1 ; i -- ) { if ( dp [ i ] != 0 ) { for ( let j = 2 * i ; j < N ; j += i ) { dp [ i ] = Math . max ( dp [ i ] , 1 + dp [ j ] ) ; ans = Math . max ( ans , dp [ i ] ) ; } } } return ans ; } let arr = [ 3 , 4 , 6 , 8 , 10 , 18 , 21 , 24 ] ; let n = arr . length ; document . write ( maximum_set ( arr , n ) ) ;