Compute optimized lds values in bottom up manner
for ( int i = 1 ; i < n ; i ++ ) { lds [ i ] = 1 ; for ( int j = 0 ; j < i ; j ++ ) if ( lds [ j ] != 0 && arr [ i ] % arr [ j ] == 0 ) lds [ i ] = max ( lds [ i ] , lds [ j ] + 1 ) ; }