text: Longest subsequence from an array of pairs having first element increasing and second element decreasing . | C # 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
code: using System ; class GFG { 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 ) ; } } } Console . Write ( dp [ N - 1 ] ) ; } static void Main ( ) { int [ , ] A = { { 1 , 2 } , { 2 , 2 } , { 3 , 1 } } ; int N = A . GetLength ( 0 ) ; longestSubSequence ( A , N ) ; } }