Longest chain of pairs
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
The longest chain of pairs problem asks you to find the maximum number of pairs (a, b) you can chain together so that each next pair starts after the previous pair ends. Formally, given pairs where each has a start a and end b, find the longest sequence where b_i < a_{i+1} for every consecutive pair. This is equivalent to the activity selection problem and appears in scheduling, bioinformatics sequence alignment, and interval optimization.
Problem Definition
Given n pairs where each pair is (a, b) with a < b, select the longest subsequence of pairs such that for consecutive selected pairs (a_i, b_i) and (a_j, b_j), the condition b_i < a_j holds.
Greedy Approach (Optimal)
Sort pairs by their second element (end value), then greedily pick each pair whose start is greater than the previous selected pair's end. This is the same logic as the classic activity selection problem.
Sorting by end value ensures that each selection leaves the most room for future pairs. This runs in O(n log n) time due to sorting and O(1) extra space.
Why Sort by End, Not Start
Sorting by start value can lead to suboptimal chains. A pair with a small start but large end blocks many future pairs. Sorting by end minimizes the "footprint" of each selected pair.
Dynamic Programming Approach
If you need the actual chain (not just the length), or want to understand the DP formulation, define dp[i] as the length of the longest chain ending with pair i.
This runs in O(n^2) time and O(n) space. It is slower than greedy but generalizes to variants where the selection criterion is more complex.
Reconstructing the Chain
To recover the actual pairs in the chain, track predecessors during DP.
Comparison
| Approach | Time | Space | Returns chain? |
| Greedy | O(n log n) | O(1) | Length only |
| DP | O(n^2) | O(n) | Length + chain |
Use greedy when you only need the count. Use DP when you need the actual selected pairs or when the selection rule has additional constraints.
Common Pitfalls
- Sorting by the first element instead of the second, which produces suboptimal greedy results.
- Using
>=instead of>in the chain condition — the problem requires strict inequalityb_i < a_j, notb_i <= a_j. - Forgetting to handle the single-pair base case where the answer is always 1.
- Confusing this with Longest Increasing Subsequence — LIS operates on single values, not pairs with start/end.
- Returning
dp[-1]instead ofmax(dp)— the longest chain may not end at the last sorted pair.
Summary
- Sort pairs by end value and apply greedy selection for O(n log n) optimal length.
- Use DP with O(n^2) time when you need to reconstruct the actual chain or handle custom constraints.
- The problem is equivalent to activity selection and maximum non-overlapping intervals.
- Always use strict inequality
b_i < a_jfor chain validity. - Greedy by end value is provably optimal for the standard formulation.

