Largest subsequence such that all indices and all values are multiples individually | Python3 program for the above approach ; Function that print 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 ; Printing maximum length of array ; Driver Code ; Given array arr [ ] ; Function call
from math import * NEW_LINE def maxLength ( arr , n ) : NEW_LINE INDENT dp = [ 1 ] * n NEW_LINE for i in range ( n - 1 , 1 , - 1 ) : NEW_LINE INDENT for j in range ( 1 , int ( sqrt ( i ) ) + 1 ) : NEW_LINE INDENT if ( i % j == 0 ) : NEW_LINE INDENT s = i // j NEW_LINE if ( s == j ) : NEW_LINE INDENT if ( arr [ i ] > arr [ s ] ) : NEW_LINE INDENT dp [ s ] = max ( dp [ i ] + 1 , dp [ s ] ) NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT if ( s != i and arr [ i ] > arr [ s ] ) : NEW_LINE INDENT dp [ s ] = max ( dp [ i ] + 1 , dp [ s ] ) NEW_LINE DEDENT if ( arr [ i ] > arr [ j ] ) : NEW_LINE INDENT dp [ j ] = max ( dp [ i ] + 1 , dp [ j ] ) NEW_LINE DEDENT DEDENT DEDENT DEDENT DEDENT Max = 0 NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if ( dp [ i ] > Max ) : NEW_LINE INDENT Max = dp [ i ] NEW_LINE DEDENT DEDENT print ( Max ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 0 , 1 , 4 , 2 , 3 , 6 , 4 , 9 ] NEW_LINE size = len ( arr ) NEW_LINE maxLength ( arr , size ) NEW_LINE DEDENT