Longest subsequence from an array of pairs having first element increasing and second element decreasing . | Java program for the above approach ; Function to find the length of the longest subsequence of pairs whose first element is increasing and second is decreasing ; dp [ i ] : Stores the longest subsequence upto i ; Base case ; When the conditions hold ; Finally , print the required answer ; Driver Code ; Given Input ; Function Call
class GFG { public static void longestSubSequence ( int [ ] [ ] A , int N ) { int [ ] dp = new int [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { dp [ i ] = 1 ; for ( int j = 0 ; j < i ; j ++ ) { if ( A [ j ] [ 0 ] < A [ i ] [ 0 ] && A [ j ] [ 1 ] > A [ i ] [ 1 ] ) { dp [ i ] = Math . max ( dp [ i ] , dp [ j ] + 1 ) ; } } } System . out . println ( dp [ N - 1 ] ) ; } public static void main ( String args [ ] ) { int [ ] [ ] A = { { 1 , 2 } , { 2 , 2 } , { 3 , 1 } } ; int N = A . length ; longestSubSequence ( A , N ) ; } }