The longest increasing subsequence problem is to find a subsequence of a given sequence in which the subsequence’s elements are in sorted order, lowest to highest, and in which the subsequence is as long as possible. This subsequence is not necessarily contiguous or unique.

Please note that the problem specifically targets subsequences that need not be contiguous, i.e., subsequences are not required to occupy consecutive positions within the original sequences.

For example, the longest increasing subsequence of [0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15] is [0, 2, 6, 9, 11, 15].

This subsequence has length 6; the input sequence has no 7–member increasing subsequences. The longest increasing subsequence in this example is not unique.

For instance, [0, 4, 6, 9, 11, 15] or [0, 4, 6, 9, 13, 15] are other increasing subsequences of equal length in the same input sequence.
 

Practice this problem

 
The idea is to use recursion to solve this problem. For each item, there are two possibilities:

  1. Include the current item in LIS if it is greater than the previous element in LIS and recur for the remaining items.
  2. Exclude the current item from LIS and recur for the remaining items.

Finally, return the maximum value we get by including or excluding the current item. The base case of the recursion would be when no items are left. Following is the C++, Java, and Python implementation of the idea:

C++


Download  Run Code

Output:

The length of the LIS is 6

Java


Download  Run Code

Output:

The length of the LIS is 6

Python


Download  Run Code

Output:

The length of the LIS is 6

The time complexity of the above solution is exponential and occupies space in the call stack.
 

 
The problem has an optimal substructure. That means the problem can be broken down into smaller, simple “subproblems”, which can further be divided into yet simpler, smaller subproblems until the solution becomes trivial. The above solution also exhibits overlapping subproblems. If we draw the solution’s recursion tree, we can see that the same subproblems are repeatedly computed. We know that problems with optimal substructure and overlapping subproblems can be solved using dynamic programming, where subproblem solutions are memoized rather than computed repeatedly. The memoized version follows the top-down approach since we first break the problem into subproblems and then calculate and store values.

 
We can also solve this problem in a bottom-up manner. In the bottom-up approach, solve smaller subproblems first, then solve larger subproblems from them. The following bottom-up approach computes L[i], for each 0 <= i < n, which stores the length of the longest increasing subsequence of subarray arr[0…i] that ends with arr[i]. To calculate L[i], consider LIS of all smaller values of i (say j) already computed and pick maximum L[j], where arr[j] is less than the current element arr[i]. It has the same asymptotic runtime as Memoization but no recursion overhead.

Following is the C++, Java, and Python implementation of the idea:

C++


Download  Run Code

Output:

The length of the LIS is 6

Java


Download  Run Code

Output:

The length of the LIS is 6

Python


Download  Run Code

Output:

The length of the LIS is 6

The time complexity of the above solution is O(n2) and requires O(n) extra space, where n is the size of the given sequence.

How to print LIS?

The above-discussed methods only print the length of LIS but do not print LIS itself. To print the LIS, we have to store the longest increasing subsequence in the lookup table instead of storing just the LIS length.

For example, consider array arr = [ 0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15]. The longest increasing subsequence of subarray arr[0…i] that ends with arr[i] are:

LIS[0] — 0
LIS[1] — 0 8
LIS[2] — 0 4
LIS[3] — 0 8 12
LIS[4] — 0 2
LIS[5] — 0 8 10
LIS[6] — 0 4 6
LIS[7] — 0 8 12 14
LIS[8] — 0 1
LIS[9] — 0 4 6 9
LIS[10] — 0 4 5
LIS[11] — 0 4 6 9 13
LIS[12] — 0 2 3
LIS[13] — 0 4 6 9 11
LIS[14] — 0 4 6 7
LIS[15] — 0 4 6 9 13 15

Following is the C++, Java, and Python implementation of the idea:

C++


Download  Run Code

Output:

0 4 6 9 13 15

Java


Download  Run Code

Output:

[0, 4, 6, 9, 13, 15]

Python


Download  Run Code

Output:

[0, 4, 6, 9, 13, 15]

The time complexity of the above solution is O(n2) and requires O(n2) extra space, where n is the size of the given sequence.

 
Refer to the following post for the O(n.log(n)) solution to find the length and print longest increasing subsequence.

Longest Increasing Subsequence Problem

 
References:

1. Longest increasing subsequence – Wikipedia.

2. Dynamic Programming #1: Longest Increasing subsequence – YouTube.