Longest increasing subsequence consisting of elements from indices divisible by previously selected indices | Function to find length of longest subsequence generated that satisfies the specified conditions ; Stores the length of longest subsequences of all lengths ; Iterate through the given array ; Iterate through the multiples i ; Update dp [ j ] as maximum of dp [ j ] and dp [ i ] + 1 ; Return the maximum element in dp as the length of longest subsequence ; Driver Code ; Function Call
function findMaxLength ( N , arr ) { var dp = Array ( N + 1 ) . fill ( 1 ) ; for ( i = 1 ; i <= N ; i ++ ) { for ( j = 2 * i ; j <= N ; j += i ) { if ( arr [ i - 1 ] < arr [ j - 1 ] ) { dp [ j ] = Math . max ( dp [ j ] , dp [ i ] + 1 ) ; } } } return Math . max . apply ( Math , dp ) ; } var arr = [ 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ] ; var N = arr . length ; document . write ( findMaxLength ( N , arr ) ) ;