Maximum Length Pair Chain Problem
You are provided with 'N' pairs of integers, where the first number in each pair is less than the second number, i.e., in pair (a, b) -> a < b. A pair chain is defined as a sequence of pairs such that pair (c, d) can follow pair (a, b) only if b < c.
Your task is to determine the length of the longest pair chain that can be formed using the provided pairs.
Example:
Input:
Pairs = [[3, 4], [1, 2], [2, 3]]
Output:
2
Explanation:
The longest chain is [1, 2] -> [3, 4], making the length 2.
Constraints:
- You can select a pair only once.
- It is not necessary to use all the given pairs.
- You can rearrange pairs in any order.
Input:
The first line contains an integer 'T' representing the number of test cases.
Each test case starts with an integer 'N' which indicates the number of pairs.
The next 'N' lines contain two space-separated integers 'a' and 'b', representing each pair.
Output:
For each test case, return an integer that denotes the maximum length of the chain that satisfies the given condition.
The output should be on a separate line for each test case.
Constraints:
- 1 <= T <= 100
- 1 <= N <= 10^4
- -10^9 <= a, b <= 10^9
- Time Limit: 1 second
AnswerBot
5d
Find the length of the longest pair chain that can be formed using the provided pairs.
Sort the pairs based on the second number in each pair.
Iterate through the sorted pairs and keep track of the maxi...read more
Help your peers!
Add answer anonymously...
Top Cadence Design Systems SDE-2 interview questions & answers
Popular interview questions of SDE-2
>
Cadence Design Systems SDE-2 Interview Questions
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app