// C# program for the above approach
using System;
class GFG{
// Recursive function to find the length of
// the longest subsequence of pairs whose first
// element is increasing and second is decreasing
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);
// Base case
if (ind == N)
return 0;
// Not include the current pair
// in the longest subsequence
int ans = longestSubSequence(A, N, ind + 1,
lastf, lasts);
// Including the current pair
// in the longest subsequence
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);
}
// Driver Code
public static void Main()
{
// Given Input
int[,] A = { { 1, 2 }, { 2, 2 }, { 3, 1 } };
int N = A.GetLength(0);
// Function Call
Console.Write(longestSubSequence(A, N));
}
}
// This code is contributed by target_2.