Find the Largest divisor Subset in the Array | Function to find the required subsequence ; Sort the array ; Keep a count of the length of the subsequence and the previous element ; Set the initial values ; Maximum length of the subsequence and the last element ; Run a loop for every element ; Check for all the divisors ; If the element is a divisor and the length of subsequence will increase by adding j as previous element of i ; Increase the count ; Update the max count ; Get the last index of the subsequence ; Print the element ; Move the index to the previous element ; Driver Code
function findSubSeq ( arr , n ) { arr . sort ( ) ; var count = new Array ( n ) ; var prev = new Array ( n ) ; count . fill ( 1 ) ; prev . fill ( - 1 ) ; var max = 0 ; var maxprev = - 1 ; for ( var i = 0 ; i < n ; i ++ ) { for ( var j = i - 1 ; j >= 0 ; j -- ) { if ( arr [ i ] % arr [ j ] == 0 && count [ j ] + 1 > count [ i ] ) { count [ i ] = count [ j ] + 1 ; prev [ i ] = j ; } } if ( max < count [ i ] ) { max = count [ i ] ; maxprev = i ; } } var i = maxprev ; while ( i >= 0 ) { if ( arr [ i ] != - 1 ) document . write ( arr [ i ] + " " ) ; i = prev [ i ] ; } } var arr = [ 1 , 2 , 3 , 4 , 5 ] ; var n = arr . length ; findSubSeq ( arr , n ) ;