# Construction of Longest Increasing Subsequence(LIS) and printing LIS sequence

Java code:

```The Longest Increasing Subsequence (LIS) problem is to find the length of the longest subsequence
of a given sequence such that all elements of the subsequence are sorted in increasing order.
For example, the length of LIS for {10, 22, 9, 33, 21, 50, 41, 60, 80} is 6 and LIS is {10, 22, 33, 50, 60, 80}.
```

## Java

 `package` `BIT; ` ` `  `import` `java.util.ArrayList; ` `import` `java.util.Iterator; ` ` `  `public` `class` `LongestIncreasingSubsequence { ` `    ``public` `static` `void` `main(String[] args) { ` `//        int array[] = {10, 22, 9, 33, 21, 50, 41, 60, 80}; ` `//        int array[] = {10, 2, 9, 3, 5, 4, 6, 8}; ` `        ``int` `array[] = {``10``, ``9``, ``8``, ``6``, ``5``, ``4``}; ` `        ``ArrayList list = ``new` `ArrayList(); ` `        ``ArrayList longestList = ``new` `ArrayList(); ` `        ``int` `currentMax; ` `        ``int` `highestCount = ``0``; ` `        ``for``(``int` `i = ``0``; i < array.length;i++) ` `        ``{ ` `            ``currentMax = Integer.MIN_VALUE; ` `            ``for``(``int` `j = i;j < array.length; j++) ` `            ``{ ` `                ``if``(array[j] > currentMax) ` `                ``{ ` `                    ``list.add(array[j]); ` `                    ``currentMax = array[j]; ` `                ``} ` `            ``} ` `             `  `            ``//Compare previous highest subsequence  ` `            ``if``(highestCount < list.size()) ` `            ``{ ` `                ``highestCount = list.size(); ` `                ``longestList = ``new` `ArrayList(list);   ` `            ``}    ` `            ``list.clear(); ` `        ``}  ` `        ``System.out.println(); ` `         `  `        ``//Print list ` `        ``Iterator itr = longestList.iterator(); ` `        ``System.out.println(``"The Longest subsequence"``); ` `        ``while``(itr.hasNext()) ` `        ``{ ` `            ``System.out.print(itr.next() + ``" "``); ` `        ``} ` `        ``System.out.println(); ` `        ``System.out.println(``"Length of LIS: "` `+ highestCount); ` `    ``} ` `     `  `} `

## C#

 `using` `System; ` `using` `System.Collections.Generic; ` ` `  `class` `longestIncreasingSubsequence ` `{  ` `    ``public` `static` `void` `Main(String[] args) ` `    ``{  ` `        ``int` `[]array = {10, 22, 9, 33, 21, 50, 41, 60, 80};  ` `        ``// int []array = {10, 2, 9, 3, 5, 4, 6, 8};  ` `        ``//int []array = {10, 9, 8, 6, 5, 4};  ` `        ``List<``int``> list = ``new` `List<``int``>();  ` `        ``List<``int``> longestList = ``new` `List<``int``>();  ` `        ``int` `currentMax;  ` `        ``int` `highestCount = 0;  ` `        ``for``(``int` `i = 0; i < array.Length;i++)  ` `        ``{  ` `            ``currentMax = ``int``.MinValue;  ` `            ``for``(``int` `j = i;j < array.Length; j++)  ` `            ``{  ` `                ``if``(array[j] > currentMax)  ` `                ``{  ` `                    ``list.Add(array[j]);  ` `                    ``currentMax = array[j];  ` `                ``}  ` `            ``}  ` `             `  `            ``// Compare previous highest subsequence  ` `            ``if``(highestCount < list.Count)  ` `            ``{  ` `                ``highestCount = list.Count;  ` `                ``longestList = ``new` `List<``int``>(list);  ` `            ``}  ` `            ``list.Clear();  ` `        ``}  ` `        ``Console.WriteLine();  ` `         `  `        ``// Print list  ` `        ``Console.WriteLine(``"The longest subsequence"``);  ` `        ``foreach``(``int` `itr ``in` `longestList)  ` `        ``{  ` `            ``Console.Write(itr + ``" "``);  ` `        ``}  ` `        ``Console.WriteLine();  ` `        ``Console.WriteLine(``"Length of LIS: "` `+ highestCount);  ` `    ``}  ` `}  ` ` `  `// This code is contributed by 29AjayKumar `

The longest Increasing Subsequence (LIS) problem is to find the length of the longest subsequence of a given sequence such that all elements of the subsequence are sorted in increasing order.

Examples:

```Input:  [10, 22, 9, 33, 21, 50, 41, 60, 80]
Output: [10, 22, 33, 50, 60, 80]
OR [10 22 33 41 60 80] or any other LIS of same length.
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

In previous post, we have discussed about Longest Increasing Subsequence problem. However, the post only covered code related to querying size of LIS, but not the construction of LIS. In this post, we will discuss how to print LIS using similar DP solution discussed earlier.

Let arr[0..n-1] be the input array. We define vector L such that L[i] is itself is a vector that stores LIS of arr that ends with arr[i]. For example, for array [3, 2, 6, 4, 5, 1],

```L: 3
L: 2
L: 2 6
L: 2 4
L: 2 4 5
L: 1
```

Therefore for an index i, L[i] can be recursively written as –

```L = {arr[O]}
L[i] = {Max(L[j])} + arr[i]
where j < i and arr[j] < arr[i] and if there is no such j then L[i] = arr[i]
```

Below is the implementation of above idea –

## C++

 `/* Dynamic Programming solution to construct Longest ` `   ``Increasing Subsequence */` `#include ` `#include ` `using` `namespace` `std; ` ` `  `// Utility function to print LIS ` `void` `printLIS(vector<``int``>& arr) ` `{ ` `    ``for` `(``int` `x : arr) ` `        ``cout << x << ``" "``; ` `    ``cout << endl; ` `} ` ` `  `// Function to construct and print Longest Increasing ` `// Subsequence ` `void` `constructPrintLIS(``int` `arr[], ``int` `n) ` `{ ` `    ``// L[i] - The longest increasing sub-sequence  ` `    ``// ends with arr[i] ` `    ``vector > L(n); ` ` `  `    ``// L is equal to arr ` `    ``L.push_back(arr); ` ` `  `    ``// start from index 1 ` `    ``for` `(``int` `i = 1; i < n; i++) ` `    ``{ ` `        ``// do for every j less than i ` `        ``for` `(``int` `j = 0; j < i; j++) ` `        ``{ ` `            ``/* L[i] = {Max(L[j])} + arr[i] ` `            ``where j < i and arr[j] < arr[i] */` `            ``if` `((arr[i] > arr[j]) && ` `                    ``(L[i].size() < L[j].size() + 1)) ` `                ``L[i] = L[j]; ` `        ``} ` ` `  `        ``// L[i] ends with arr[i] ` `        ``L[i].push_back(arr[i]); ` `    ``} ` ` `  `    ``// L[i] now stores increasing sub-sequence of ` `    ``// arr[0..i] that ends with arr[i] ` `    ``vector<``int``> max = L; ` ` `  `    ``// LIS will be max of all increasing sub- ` `    ``// sequences of arr ` `    ``for` `(vector<``int``> x : L) ` `        ``if` `(x.size() > max.size()) ` `            ``max = x; ` ` `  `    ``// max will contain LIS ` `    ``printLIS(max); ` `} ` ` `  `// Driver function ` `int` `main() ` `{ ` `    ``int` `arr[] = { 3, 2, 6, 4, 5, 1 }; ` `    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr); ` ` `  `    ``// construct and print LIS of arr ` `    ``constructPrintLIS(arr, n); ` ` `  `    ``return` `0; ` `} `

## Python3

 `# Dynamic Programming solution to construct Longest ` `# Increasing Subsequence ` ` `  `# Utility function to print LIS ` `def` `printLIS(arr: ``list``): ` `    ``for` `x ``in` `arr: ` `        ``print``(x, end``=``" "``) ` `    ``print``() ` ` `  `# Function to construct and print Longest Increasing ` `# Subsequence ` `def` `constructPrintLIS(arr: ``list``, n: ``int``): ` ` `  `    ``# L[i] - The longest increasing sub-sequence ` `    ``# ends with arr[i] ` `    ``l ``=` `[[] ``for` `i ``in` `range``(n)] ` ` `  `    ``# L is equal to arr ` `    ``l[``0``].append(arr[``0``]) ` ` `  `    ``# start from index 1 ` `    ``for` `i ``in` `range``(``1``, n): ` ` `  `        ``# do for every j less than i ` `        ``for` `j ``in` `range``(i): ` ` `  `            ``# L[i] = {Max(L[j])} + arr[i] ` `            ``# where j < i and arr[j] < arr[i] ` `            ``if` `arr[i] > arr[j] ``and` `(``len``(l[i]) < ``len``(l[j]) ``+` `1``): ` `                ``l[i] ``=` `l[j].copy() ` ` `  `        ``# L[i] ends with arr[i] ` `        ``l[i].append(arr[i]) ` ` `  `    ``# L[i] now stores increasing sub-sequence of ` `    ``# arr[0..i] that ends with arr[i] ` `    ``maxx ``=` `l[``0``] ` ` `  `    ``# LIS will be max of all increasing sub- ` `    ``# sequences of arr ` `    ``for` `x ``in` `l: ` `        ``if` `len``(x) > ``len``(maxx): ` `            ``maxx ``=` `x ` ` `  `    ``# max will contain LIS ` `    ``printLIS(maxx) ` ` `  `# Driver Code ` `if` `__name__ ``=``=` `"__main__"``: ` ` `  `    ``arr ``=` `[``3``, ``2``, ``6``, ``4``, ``5``, ``1``] ` `    ``n ``=` `len``(arr) ` ` `  `    ``# construct and print LIS of arr ` `    ``constructPrintLIS(arr, n) ` ` `  `# This code is contributed by ` `# sanjeev2552 `

Output:

```2 4 5
```

Note that the time complexity of the above Dynamic Programming (DP) solution is O(n^2) and there is a O(n Log n) non-DP solution for the LIS problem. See below post for O(n Log n) solution.

Construction of Longest Monotonically Increasing Subsequence (N log N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up

Article Tags :
Practice Tags :

10

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.