


Given Pairs = [3,4], [1,2], [2,3].
The length of the maximum chain will be 2. The longest chain is [1,2] -> [3,4].
1. You can select a pair only once.
2. You needn’t use up all the given pairs.
3. You can select pairs in any order.
The first line of the input contains an integer ‘T’ denoting the number of test cases.
The first line of each test case contains a single positive integer ‘N’ denoting the total number of the pairs.
Next ‘N’ lines of each test case contain two space-separated integers ‘a’ and ‘b’ denoting the elements of the pair.
For each test case, return a positive integer denoting the maximum length of the pair chain that can be possible while satisfying the given condition.
The output of each test case will be printed in a separate line.
You don’t need to print anything, it has been already taken care of. Just implement the given function.
1 <= T <= 100
1 <= N <= 10^4
-10^9 <= a,b <= 10^9
Where ‘a’ and ‘b’ is the elements of the pair.
Time Limit: 1 sec
We will solve this problem recursively by dividing it into subproblems. We first need to sort the array according to their first element. But why? as it is given in the question that you can pick any pair in any order so if you pick a pair at index ‘i’ then for the next pair you need to try all pairs from [0, ‘N’ - 1] which are not selected in the current chain that will make our implementation more difficult. So with the help of sorting the array, we ensure that our next pair will always lie after the chosen index ‘i’.
After the sorting, for each pair we have two choices: either we can include this pair in our current chain or do not include this pair in our current chain.
We will sort the array according to the first element to define an order of picking pairs in the chain. We will use a ‘DP’ table of size ‘N’ to memoize the results for each index. Definition: dp[i] will store the max length pair chain starting from index ‘i’.
If the result is already calculated then we simply return it else we will calculate the answer for the current index.
Transitions: Let our function be solve(i) then for the current pair we have 2 choices:
This problem is a variation of LIS (longest increasing subsequence). We will sort the array according to the first element to define an order of picking pairs in the chain. Now the elements are sorted according to the first elements we need to find the longest increasing subsequence according to their second elements.
We will use a ‘DP’ table of size ‘N’ to calculate the results for each index. Definition: ‘DP’[i] will store the max length pair chain ending at index ‘i’.
This problem can also be solved using the Activity selection greedy approach because it is given in the question that the first element of the pair is always less than the second element of the pair. We sort the pairs according to the second element and then we greedily add the next pair to our chain that offers the most obvious and immediate benefit i.e, the pair having the lowest second element in the remaining pairs.
Proof: Suppose we have two pairs ‘P[i]' and ‘P[j]’ where ‘P[i]’.second < ‘P[j]’.second and ‘i’ < ‘j’ but we don’t know anything about ‘P[i]’.first and ‘P[j]’.first. Now according to the greedy approach we should add ‘P[i]’ to our chain first. But why ?
Let’s check the following cases: