Given an array of pairs of numbers of size **N**. 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. The chain of pairs can be formed in this fashion. The task is to find the length of the longest chain which can be formed from a given set of pairs.

**Examples:**

Input:N = 5, arr={{5, 24}, {39, 60}, {15, 28}, {27, 40}, {50, 90} }

Output:3

The longest chain that can be formed is of length 3, and the chain is {{5, 24}, {27, 40}, {50, 90}}.

Input :N = 2, arr={{5, 10}, {1, 11}}

Output :1

** Approach: **A dynamic programming approach for the problem has been discussed here.

Idea is to solve the problem using the greedy approach which is the same as Activity Selection Problem.

- Sort all pairs in increasing order of second number of each pair.
- Select first no as the first pair of chain and set a variable s(say) with the second value of the first pair.
- Iterate from the second pair to last pair of the array and if the value of the first element of the current pair is greater then previously selected pair then select the current pair and update the value of maximum length and variable s.
- Return the value of Max length of chain.

Below is the implementation of the above approach.

`// C++ implementation of the above approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Structure for storing pairs ` `// of first and second values. ` `struct` `val { ` ` ` `int` `first; ` ` ` `int` `second; ` `}; ` ` ` `// Comparator function which can compare ` `// the second element of structure used to ` `// sort pairs in increasing order of second value. ` `bool` `comparator(` `struct` `val p1, ` `struct` `val p2) ` `{ ` ` ` `return` `(p1.second < p2.second); ` `} ` ` ` `// Function for finding max length chain ` `int` `maxChainLen(` `struct` `val p[], ` `int` `n) ` `{ ` ` ` `// Initialize length l = 1 ` ` ` `int` `l = 1; ` ` ` ` ` `// Sort all pair in increasing order ` ` ` `// according to second no of pair ` ` ` `sort(p, p + n, comparator); ` ` ` ` ` `// Pick up the first pair and assign the ` ` ` `// value of second element fo pair to a ` ` ` `// temporary variable s ` ` ` `int` `s = p[0].second; ` ` ` ` ` `// Iterate from second pair (index of ` ` ` `// the second pair is 1) to the last pair ` ` ` `for` `(` `int` `i = 1; i < n; i++) { ` ` ` ` ` `// If first of current pair is greater ` ` ` `// than previously selected pair then ` ` ` `// select current pair and update ` ` ` `// value of l and s ` ` ` `if` `(p[i].first > s) { ` ` ` `l++; ` ` ` `s = p[i].second; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Return maximum length ` ` ` `return` `l; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` ` ` `// Declaration of array of structure ` ` ` `val p[] = { { 5, 24 }, { 39, 60 }, ` ` ` `{ 15, 28 }, { 27, 40 }, { 50, 90 } }; ` ` ` ` ` `int` `n = ` `sizeof` `(p) / ` `sizeof` `(p[0]); ` ` ` ` ` `// Fucntion call ` ` ` `cout << maxChainLen(p, n) << endl; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

3

**Time complexity : ** O(N*log(N))

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.

## Recommended Posts:

- Maximum Length Chain of Pairs | DP-20
- Find the maximum cost of an array of pairs choosing at most K pairs
- Find all unique pairs of maximum and second maximum elements over all sub-arrays in O(NlogN)
- Length of longest subarray of length at least 2 with maximum GCD
- Maximum length Subsequence with alternating sign and maximum Sum
- Maximum length L such that the sum of all subarrays of length L is less than K
- Maximize product of digit sum of consecutive pairs in a subsequence of length K
- Number of pairs with maximum sum
- Maximum LCM among all pairs (i, j) from the given Array
- Find the maximum possible value of a[i] % a[j] over all pairs of i and j
- Maximum count of pairs which generate the same sum
- Maximum modulo of all the pairs of array where arr[i] >= arr[j]
- Maximum sum of pairs with specific difference
- Maximum sum of minimums of pairs in an array
- Maximum set bit count from pairs of integers from 0 to N that yields a sum as N
- Minimum and Maximum sum of absolute differences of pairs
- Maximum Sum Subsequence of length k
- Maximum sum subarray of even length
- Maximum GCD of all subarrays of length at least 2
- Maximum length of rod for Q-th person

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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.