Longest subsequence from an array of pairs having first element increasing and second element decreasing . | Java program for the above approach ; Recursive function to find the length of the longest subsequence of pairs whose first element is increasing and second is decreasing ; Base case ; Not include the current pair in the longest subsequence ; Including the current pair in the longest subsequence ; Function ; Driver Code ; Given Input ; Function Call
class GFG { public static Integer longestSubSequence ( int [ ] [ ] A , int N , int ind , int lastf , int lasts ) { ind = ( ind > 0 ? ind : 0 ) ; lastf = ( lastf > 0 ? lastf : Integer . MIN_VALUE ) ; lasts = ( lasts > 0 ? lasts : Integer . MAX_VALUE ) ; if ( ind == N ) return 0 ; int ans = longestSubSequence ( A , N , ind + 1 , lastf , lasts ) ; if ( A [ ind ] [ 0 ] > lastf && A [ ind ] [ 1 ] < lasts ) ans = Math . max ( ans , longestSubSequence ( A , N , ind + 1 , A [ ind ] [ 0 ] , A [ ind ] [ 1 ] ) + 1 ) ; return ans ; } public static int longestSubSequence ( int [ ] [ ] A , int N ) { return longestSubSequence ( A , N , 0 , 0 , 0 ) ; } public static void main ( String args [ ] ) { int [ ] [ ] A = { { 1 , 2 } , { 2 , 2 } , { 3 , 1 } } ; int N = A . length ; System . out . println ( longestSubSequence ( A , N ) ) ; } }