You are given n pairs of numbers. In every pair, the first number is always smaller than the second number. A pair (c, d) can follow another pair (a, b) if b < c. Chain of pairs can be formed in this fashion. Find the longest chain which can be formed from a given set of pairs.

**Examples:**

Input:(5, 24), (39, 60), (15, 28), (27, 40), (50, 90)Output:(5, 24), (27, 40), (50, 90)Input:(11, 20), {10, 40), (45, 60), (39, 40)Output:(11, 20), (39, 40), (45, 60)

In previous post, we have discussed about Maximum Length Chain of Pairs problem. However, the post only covered code related to finding the length of maximum size chain, but not to the construction of maximum size chain. In this post, we will discuss how to construct Maximum Length Chain of Pairs itself.

The idea is to first sort given pairs in increasing order of their first element. Let arr[0..n-1] be the input array of pairs after sorting. We define vector L such that L[i] is itself is a vector that stores Maximum Length Chain of Pairs of arr[0..i] that ends with arr[i]. Therefore for an index i, L[i] can be recursively written as –

L[0] = {arr[0]} L[i] = {Max(L[j])} + arr[i] where j < i and arr[j].b < arr[i].a = arr[i], if there is no such j

For example, for (5, 24), (39, 60), (15, 28), (27, 40), (50, 90)

L[0]: (5, 24) L[1]: (5, 24) (39, 60) L[2]: (15, 28) L[3]: (5, 24) (27, 40) L[4]: (5, 24) (27, 40) (50, 90)

Please note sorting of pairs is done as we need to find the maximum pair length and ordering doesn’t matter here. If we don’t sort, we will get pairs in increasing order but they won’t be maximum possible pairs.

Below is implementation of above idea –

## C++

`/* Dynamic Programming solution to construct ` ` ` `Maximum Length Chain of Pairs */` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `struct` `Pair ` `{ ` ` ` `int` `a; ` ` ` `int` `b; ` `}; ` ` ` `// comparator function for sort function ` `int` `compare(Pair x, Pair y) ` `{ ` ` ` `return` `x.a < y.a; ` `} ` ` ` `// Function to construct Maximum Length Chain ` `// of Pairs ` `void` `maxChainLength(vector<Pair> arr) ` `{ ` ` ` `// Sort by start time ` ` ` `sort(arr.begin(), arr.end(), compare); ` ` ` ` ` `// L[i] stores maximum length of chain of ` ` ` `// arr[0..i] that ends with arr[i]. ` ` ` `vector<vector<Pair> > L(arr.size()); ` ` ` ` ` `// L[0] is equal to arr[0] ` ` ` `L[0].push_back(arr[0]); ` ` ` ` ` `// start from index 1 ` ` ` `for` `(` `int` `i = 1; i < arr.size(); i++) ` ` ` `{ ` ` ` `// for every j less than i ` ` ` `for` `(` `int` `j = 0; j < i; j++) ` ` ` `{ ` ` ` `// L[i] = {Max(L[j])} + arr[i] ` ` ` `// where j < i and arr[j].b < arr[i].a ` ` ` `if` `((arr[j].b < arr[i].a) && ` ` ` `(L[j].size() > L[i].size())) ` ` ` `L[i] = L[j]; ` ` ` `} ` ` ` `L[i].push_back(arr[i]); ` ` ` `} ` ` ` ` ` `// print max length vector ` ` ` `vector<Pair> maxChain; ` ` ` `for` `(vector<Pair> x : L) ` ` ` `if` `(x.size() > maxChain.size()) ` ` ` `maxChain = x; ` ` ` ` ` `for` `(Pair pair : maxChain) ` ` ` `cout << ` `"("` `<< pair.a << ` `", "` ` ` `<< pair.b << ` `") "` `; ` `} ` ` ` `// Driver Function ` `int` `main() ` `{ ` ` ` `Pair a[] = {{5, 29}, {39, 40}, {15, 28}, ` ` ` `{27, 40}, {50, 90}}; ` ` ` `int` `n = ` `sizeof` `(a)/` `sizeof` `(a[0]); ` ` ` ` ` `vector<Pair> arr(a, a + n); ` ` ` ` ` `maxChainLength(arr); ` ` ` ` ` `return` `0; ` `} ` |

## Python3

`# Dynamic Programming solution to construct ` `# Maximum Length Chain of Pairs ` `class` `Pair: ` ` ` ` ` `def` `__init__(` `self` `, a, b): ` ` ` `self` `.a ` `=` `a ` ` ` `self` `.b ` `=` `b ` ` ` ` ` `def` `__lt__(` `self` `, other): ` ` ` `return` `self` `.a < other.a ` ` ` `def` `maxChainLength(arr): ` ` ` ` ` `# Function to construct ` ` ` `# Maximum Length Chain of Pairs ` ` ` ` ` `# Sort by start time ` ` ` `arr.sort() ` ` ` ` ` `# L[i] stores maximum length of chain of ` ` ` `# arr[0..i] that ends with arr[i]. ` ` ` `L ` `=` `[[] ` `for` `x ` `in` `range` `(` `len` `(arr))] ` ` ` ` ` `# L[0] is equal to arr[0] ` ` ` `L[` `0` `].append(arr[` `0` `]) ` ` ` ` ` `# start from index 1 ` ` ` `for` `i ` `in` `range` `(` `1` `, ` `len` `(arr)): ` ` ` ` ` `# for every j less than i ` ` ` `for` `j ` `in` `range` `(i): ` ` ` ` ` `# L[i] = {Max(L[j])} + arr[i] ` ` ` `# where j < i and arr[j].b < arr[i].a ` ` ` `if` `(arr[j].b < arr[i].a ` `and` ` ` `len` `(L[j]) > ` `len` `(L[i])): ` ` ` `L[i] ` `=` `L[j] ` ` ` ` ` `L[i].append(arr[i]) ` ` ` ` ` `# print max length vector ` ` ` `maxChain ` `=` `[] ` ` ` `for` `x ` `in` `L: ` ` ` `if` `len` `(x) > ` `len` `(maxChain): ` ` ` `maxChain ` `=` `x ` ` ` ` ` `for` `pair ` `in` `maxChain: ` ` ` `print` `(` `"({a},{b})"` `.` `format` `(a ` `=` `pair.a, ` ` ` `b ` `=` `pair.b), ` ` ` `end ` `=` `" "` `) ` ` ` `print` `() ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` `arr ` `=` `[Pair(` `5` `, ` `29` `), Pair(` `39` `, ` `40` `), ` ` ` `Pair(` `15` `, ` `28` `), Pair(` `27` `, ` `40` `), ` ` ` `Pair(` `50` `, ` `90` `)] ` ` ` `n ` `=` `len` `(arr) ` ` ` `maxChainLength(arr) ` ` ` `# This code is contributed ` `# by vibhu4agarwal ` |

**Output:**

(5, 29) (39, 40) (50, 90)

**Time complexity** of above Dynamic Programming solution is O(n^{2}) where n is the number of pairs.

**Auxiliary space** used by the program is O(n^{2}).

This article is contributed by **Aditya Goel**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.