for every j greater than i
for ( int j = n - 1 ; j > i ; j -- ) { if ( arr [ j ] < arr [ i ] && LDS [ j ] . size ( ) > LDS [ i ] . size ( ) ) for ( int k : LDS [ j ] ) if ( ! LDS [ i ] . contains ( k ) ) LDS [ i ] . add ( k ) ; } LDS [ i ] . add ( arr [ i ] ) ; }