Longest Increasing Subsequence using Longest Common Subsequence Algorithm | Java implementation of above approach ; Function to return the size of the longest increasing subsequence ; Create an 2D array of integer for tabulation ; Take the second sequence as the sorted sequence of the given sequence ; Classical Dynamic Programming algorithm for Longest Common Subsequence ; Return the ans ; Driver code
import java . util . * ; class GFG { static int LISusingLCS ( Vector < Integer > seq ) { int n = seq . size ( ) ; int L [ ] [ ] = new int [ n + 1 ] [ n + 1 ] ; Vector < Integer > sortedseq = new Vector < Integer > ( seq ) ; Collections . sort ( sortedseq ) ; for ( int i = 0 ; i <= n ; i ++ ) { for ( int j = 0 ; j <= n ; j ++ ) { if ( i == 0 j == 0 ) L [ i ] [ j ] = 0 ; else if ( seq . get ( i - 1 ) == sortedseq . get ( j - 1 ) ) L [ i ] [ j ] = L [ i - 1 ] [ j - 1 ] + 1 ; else L [ i ] [ j ] = Math . max ( L [ i - 1 ] [ j ] , L [ i ] [ j - 1 ] ) ; } } return L [ n ] [ n ] ; } public static void main ( String args [ ] ) { Vector < Integer > sequence = new Vector < Integer > ( ) ; sequence . add ( 12 ) ; sequence . add ( 34 ) ; sequence . add ( 1 ) ; sequence . add ( 5 ) ; sequence . add ( 40 ) ; sequence . add ( 80 ) ; System . out . println ( LISusingLCS ( sequence ) ) ; } }