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
4mo

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!
Select
Add answer anonymously...

Top SDE-2 Interview Questions Asked at Cadence Design Systems

Q. Design a data structure that supports the following two operations: void addNum(...read more
Q. How would you sort an array based on a user-defined order?
Q. What is a copy constructor?
SDE-2 Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits