text: Longest subsequence from an array of pairs having first element increasing and second element decreasing . | C # 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 ; Driver Code ; Given Input ; Function Call
code: using System ; class GFG { public static int longestSubSequence ( int [ , ] A , int N , int ind , int lastf , int lasts ) { ind = ( ind > 0 ? ind : 0 ) ; lastf = ( lastf > 0 ? lastf : Int32 . MinValue ) ; lasts = ( lasts > 0 ? lasts : Int32 . MaxValue ) ; 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 void Main ( ) { int [ , ] A = { { 1 , 2 } , { 2 , 2 } , { 3 , 1 } } ; int N = A . GetLength ( 0 ) ; Console . Write ( longestSubSequence ( A , N ) ) ; } }