STEVE LIS

This is another version of the longest increasing subsequence algorithm, attributed to Steve H. For LDS, make the array negative and change the invariant (Integer.MIN_VALUE and +1 will work, fix the "if" statements also to check for +1, not Integer.MAX_VALUE).

It runs O (n log n), but has an additional interesting advantage. This algorithm creates a Hashtable DP that allows you to find out what the longest increasing subsequence is, starting at (and including) a particular item in the sequence.

Java
int[] hax = new int[N] ;//the original array on which we will do LIS int[] x = new int[N+3] ; Arrays.fill(x,-1) ; x[N+1] = Integer.MAX_VALUE ; x[N+2] = Integer.MAX_VALUE ;// probably not necessary, but it guarantees that it never crashes :D for (int i = 0 ; i &lt; N ; i ++) {    hax[i] = input.nextInt ; } Hashtable DP = new Hashtable ; for (int i = N-1 ; i &gt;= 0 ; i --) {    int t = Arrays.binarySearch(x, hax[i]) ; if (t &lt; 0) t = -t ; t -=2 ; x[t] = hax[i] ; if (x[t+1] == Integer.MAX_VALUE) {        DP.put(hax[i], 1) ; }    else {        int f = (Integer)DP.get(x[t+1]) ; DP.put(hax[i], f+1) ; } } // to get the overall LIS, use (Integer)DP.get(hax[0]) ; // to get the LIS starting at some i, use (Integer)DP.get(hax[i]) ; // This algorithm is technically n log n, but it's a slow n log n because of the hashtable. If the input is nice (e.g. // all the numbers are in the range {1,2,..n}, then you can use an array which is much faster.) After running the STEVE LIS, one can then grab any value out of the Hashtable to find the LIS starting at hax[i] and necessarily including hax[i].