Largest subsequence such that all indices and all values are multiples individually | Function that prlet maximum length of array ; dp [ ] array to store the maximum length ; Find all divisors of i ; If the current value is greater than the divisor 's value ; If current value is greater than the divisor 's value  and s is not equal  to current index ; Condition if current value is greater than the divisor 's value ; Computing the greatest value ; Prleting maximum length of array ; Given array arr [ ] ; Function call
function maxLength ( arr , n ) { let dp = [ ] ; for ( let i = 1 ; i < n ; i ++ ) { dp [ i ] = 1 ; } for ( let i = n - 1 ; i > 1 ; i -- ) { for ( let j = 1 ; j <= Math . sqrt ( i ) ; j ++ ) { if ( i % j == 0 ) { let s = i / j ; if ( s == j ) { if ( arr [ i ] > arr [ s ] ) { dp [ s ] = Math . max ( dp [ i ] + 1 , dp [ s ] ) ; } } else { if ( s != i && arr [ i ] > arr [ s ] ) dp [ s ] = Math . max ( dp [ i ] + 1 , dp [ s ] ) ; if ( arr [ i ] > arr [ j ] ) { dp [ j ] = Math . max ( dp [ i ] + 1 , dp [ j ] ) ; } } } } } let max = 0 ; for ( let i = 1 ; i < n ; i ++ ) { if ( dp [ i ] > max ) max = dp [ i ] ; } document . write ( max ) ; } let arr = [ 0 , 1 , 4 , 2 , 3 , 6 , 4 , 9 ] ; let size = arr . length ; maxLength ( arr , size ) ;