Timer is Running

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. Your task is to complete the function **maxChainLen** which returns an integer denoting the longest chain which can be formed from a given set of pairs.

**Input:**

The first line of input contains an integer T denoting the no of test cases then T test cases follow .Then T test cases follow . The first line of input contains an integer N denoting the no of pairs . In the next line are 2*N space separated values denoting N pairs.

**Output:**

For each test case output will be the length of the longest chain formed.

**Your Task:**

Complete the function **maxChainLen()** which takes a structure p[] denoting the pairs and n as the number of pairs and returns the length of the longest chain formed.

**Expected Time Complexity**: O(N*N)

**Expected Auxiliary Space**: O(N)

**Constraints:**

1<=T<=100

1<=N<=100

**Example(To be used only for expected output):
Input**

2

5

5 24 39 60 15 28 27 40 50 90

2

5 10 1 11

3

1

(i) the given pairs are {{5, 24}, {39, 60}, {15, 28}, {27, 40}, {50, 90} },the longest chain that can be formed is of length 3, and the chain is {{5, 24}, {27, 40}, {50, 90}}

(ii) The max length chain possible is only of length one.

Author: Shubham Joshi 1

If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.

saiujwal13083 | 269 |

ppsvikhyat17 | 248 |

vt9941 | 216 |

generic_coder | 192 |

balaji22 | 191 |

saiujwal13083 | 528 |

ppsvikhyat17 | 436 |

generic_coder | 424 |

vt9941 | 340 |

SourabhPatel | 288 |

blackshadows | 5444 |

Ibrahim Nash | 5438 |

akhayrutdinov | 5111 |

mb1973 | 4976 |

Quandray | 4752 |

Login to report an issue on this page.