Construction of Longest Increasing Subsequence ( LIS ) and printing LIS sequence | Dynamic Programming solution to construct Longest Increasing Subsequence ; Utility function to print LIS ; Function to construct and print Longest Increasing Subsequence ; L [ i ] - The longest increasing sub - sequence ends with arr [ i ] ; L [ 0 ] is equal to arr [ 0 ] ; start from index 1 ; do for every j less than i ; L [ i ] = { Max ( L [ j ] ) } + arr [ i ] where j < i and arr [ j ] < arr [ i ] ; L [ i ] ends with arr [ i ] ; L [ i ] now stores increasing sub - sequence of arr [ 0. . i ] that ends with arr [ i ] ; LIS will be max of all increasing sub - sequences of arr ; max will contain LIS ; Driver code ; construct and print LIS of arr
using System ; using System . Collections . Generic ; class GFG { static void printLIS ( List < int > arr ) { foreach ( int x in arr ) { Console . Write ( x + " ▁ " ) ; } Console . WriteLine ( ) ; } static void constructPrintLIS ( int [ ] arr , int n ) { List < List < int > > L = new List < List < int > > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { L . Add ( new List < int > ( ) ) ; } L [ 0 ] . Add ( arr [ 0 ] ) ; for ( int i = 1 ; i < n ; i ++ ) { for ( int j = 0 ; j < i ; j ++ ) { if ( ( arr [ i ] > arr [ j ] ) && ( L [ i ] . Count < L [ j ] . Count + 1 ) ) L [ i ] = L [ j ] ; } L [ i ] . Add ( arr [ i ] ) ; } List < int > max = L [ 0 ] ; foreach ( List < int > x in L ) { if ( x . Count > max . Count ) { max = x ; } } printLIS ( max ) ; } static void Main ( ) { int [ ] arr = { 3 , 2 , 4 , 5 , 1 } ; int n = arr . Length ; constructPrintLIS ( arr , n ) ; } }