Longest subsequence from an array of pairs having first element increasing and second element decreasing . | 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 ; Given Input ; Function Call
function longestSubSequence ( A , N ) { let dp = new Array ( N ) ; for ( let i = 0 ; i < N ; i ++ ) { dp [ i ] = 1 ; for ( let 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 ) ; } } } document . write ( dp [ N - 1 ] + " " ) ; } let A = [ [ 1 , 2 ] , [ 2 , 2 ] , [ 3 , 1 ] ] ; let N = A . length ; longestSubSequence ( A , N ) ;