Maximum Length Chain of Pairs | Set-2
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++
#include <bits/stdc++.h>
using namespace std;
const bool comparator( const pair< int , int >& p1, const pair< int , int >& p2)
{
return (p1.second < p2.second);
}
int maxChainLen(vector<pair< int , int > >p, int n)
{
int l = 1;
sort(p.begin(),p.end(),comparator);
int s = p[0].second;
for ( int i = 1; i < n; i++) {
if (p[i].first > s) {
l++;
s = p[i].second;
}
}
return l;
}
int main()
{
vector<pair< int , int >> p = { { 5, 24 }, { 39, 60 },
{ 15, 28 }, { 27, 40 }, { 50, 90 } };
int n = p.size();
cout << maxChainLen(p, n) << endl;
return 0;
}
|
Java
import java.util.*;
class GFG{
static class Pair
{
int first;
int second;
Pair( int first, int second)
{
this .first = first;
this .second = second;
}
};
static int maxChainLen(Pair p[], int n)
{
int l = 1 ;
Arrays.sort(p, new Comparator<Pair>()
{
public int compare(Pair a, Pair b)
{
return a.second - b.second;
}
});
int s = p[ 0 ].second;
for ( int i = 1 ; i < n; i++)
{
if (p[i].first > s)
{
l++;
s = p[i].second;
}
}
return l;
}
public static void main(String args[])
{
Pair p[] = new Pair[ 5 ];
p[ 0 ] = new Pair( 5 , 24 );
p[ 1 ] = new Pair( 39 , 60 );
p[ 2 ] = new Pair( 15 , 28 );
p[ 3 ] = new Pair( 27 , 40 );
p[ 4 ] = new Pair( 50 , 90 );
int n = p.length;
System.out.println(maxChainLen(p, n));
}
}
|
C#
using System;
using System.Linq;
class GFG{
class Pair : IComparable<Pair>
{
public int first, second;
public Pair( int first, int second)
{
this .first = first;
this .second = second;
}
public int CompareTo(Pair p)
{
return this .second-p.second;
}
}
static int maxChainLen(Pair []p, int n)
{
int l = 1;
Array.Sort(p);
int s = p[0].second;
for ( int i = 1; i < n; i++)
{
if (p[i].first > s)
{
l++;
s = p[i].second;
}
}
return l;
}
public static void Main(String []args)
{
Pair []p = new Pair[5];
p[0] = new Pair(5, 24);
p[1] = new Pair(39, 60);
p[2] = new Pair(15, 28);
p[3] = new Pair(27, 40);
p[4] = new Pair(50, 90);
int n = p.Length;
Console.WriteLine(maxChainLen(p, n));
}
}
|
Python3
def maxChainLen(p, n):
l = 1
p.sort(key = lambda x:x[ 1 ])
s = p[ 0 ][ 1 ]
for i in range (n):
if (p[i][ 0 ] > s) :
l + = 1
s = p[i][ 1 ]
return l
if __name__ = = '__main__' :
p = [( 5 , 24 ) , ( 39 , 60 ) ,
( 15 , 28 ) , ( 27 , 40 ) , ( 50 , 90 )]
n = len (p)
print (maxChainLen(p, n))
|
Javascript
<script>
function maxChainLen(p, n){
let l = 1
p.sort((x,y)=>x[1]-y[1])
let s = p[0][1]
for (let i=0;i<n;i++){
if (p[i][0] > s){
l+=1
s = p[i][1]
}
}
return l
}
let p = [[5, 24] , [39, 60] , [15, 28] , [27, 40] , [50, 90]]
let n = p.length
document.write(maxChainLen(p, n), "</br>" )
</script>
|
Time complexity : O(N*log(N))
Auxiliary Space: O(1)
Last Updated :
19 May, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...