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
def findMaxLength ( N , arr ) : NEW_LINE INDENT dp = [ 1 ] * ( N + 1 ) NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT for j in range ( 2 * i , N + 1 , i ) : NEW_LINE INDENT if ( arr [ i - 1 ] < arr [ j - 1 ] ) : NEW_LINE INDENT dp [ j ] = max ( dp [ j ] , dp [ i ] + 1 ) NEW_LINE DEDENT DEDENT DEDENT return max ( dp ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ] NEW_LINE N = len ( arr ) NEW_LINE print ( findMaxLength ( N , arr ) ) NEW_LINE DEDENT