Print all Longest dividing subsequence in an Array | Function to print LDS [ i ] element ; Traverse the Max [ ] ; Function to construct and print Longest Dividing Subsequence ; 2D vector for storing sequences ; Push the first element to LDS [ ] [ ] ; Interate over all element ; Loop for every index till i ; If current elements divides arr [ i ] and length is greater than the previous index , then insert the current element to the sequences LDS [ i ] ; L [ i ] ends with arr [ i ] ; LDS stores the sequences till each element of arr [ ] Traverse the LDS [ ] [ ] to find the maximum length ; Print all LDS with maximum length ; Find size ; If size = maxLength ; Print LDS ; Driver Code ; Function call
def printLDS ( Max ) : NEW_LINE INDENT for it in Max : NEW_LINE INDENT print ( it , end = " ▁ " ) NEW_LINE DEDENT DEDENT def LongestDividingSeq ( arr , N ) : NEW_LINE INDENT LDS = [ [ ] for i in range ( N ) ] NEW_LINE LDS [ 0 ] . append ( arr [ 0 ] ) NEW_LINE for i in range ( 1 , N ) : NEW_LINE INDENT for j in range ( i ) : NEW_LINE INDENT if ( ( arr [ i ] % arr [ j ] == 0 ) and ( len ( LDS [ i ] ) < len ( LDS [ j ] ) + 1 ) ) : NEW_LINE INDENT LDS [ i ] = LDS [ j ] . copy ( ) NEW_LINE DEDENT DEDENT LDS [ i ] . append ( arr [ i ] ) NEW_LINE DEDENT maxLength = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT x = len ( LDS [ i ] ) NEW_LINE maxLength = max ( maxLength , x ) NEW_LINE DEDENT for i in range ( N ) : NEW_LINE INDENT size = len ( LDS [ i ] ) NEW_LINE if ( size == maxLength ) : NEW_LINE INDENT printLDS ( LDS [ i ] ) NEW_LINE print ( ) NEW_LINE DEDENT DEDENT DEDENT arr = [ 2 , 11 , 16 , 12 , 36 , 60 , 71 ] NEW_LINE N = len ( arr ) NEW_LINE LongestDividingSeq ( arr , N ) ; NEW_LINE