for every j greater than i
for ( int j = n - 1 ; j > i ; j -- ) { if ( arr [ j ] < arr [ i ] && LDS [ j ] . Count > LDS [ i ] . Count ) foreach ( int k in LDS [ j ] ) if ( ! LDS [ i ] . Contains ( k ) ) LDS [ i ] . Add ( k ) ; } LDS [ i ] . Add ( arr [ i ] ) ; }