Given K sorted lists of integers of size N each, find the smallest range that includes at least one element from each of the K lists. If more than one such range's are found, print the first such range found.

**Input:**

The first line of the input contains a single integer T denoting the number of test cases. For each test, inputs will be given to the **findSmallestRange** function. The input's to the function are:

1.) A 2D-array(arr) of size K*N, where each row represents an individual list.

2.) An integer N denoting the size of the list.

3.) An integer K denoting the number of lists.

**Output:**

For each test case output in a single line with two space-separated integers denoting the required range.

**Constraints:**

1 <= T <= 50

1 <= K,N <= 500

**Example:**

**Input:**

2

5 3

1 3 5 7 9 0 2 4 6 8 2 3 5 7 11

4 3

1 2 3 4 5 6 7 8 9 10 11 12

**Output:**

1 2

4 9

**Explanation:
Testcase1:** K = 3

A:[1 3 5 7 9]

B:[0 2 4 6 8]

C:[2 3 5 7 11]

Smallest range is formed by number 1 present in first list and 2 is present in both 2nd and 3rd list.

Author: harshitsidhwa

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.

VISHESHA SADU | 27 |

ghanshyam648 | 25 |

Biswajit3 | 21 |

karthik_kasaragadda | 20 |

tanya_sharma | 18 |

KshatriyaYash | 1066 |

nikhil_sojan | 978 |

lonecoder | 935 |

mazumderrohit8 | 864 |

SumitSingh27 | 832 |

blackshadows | 5327 |

Ibrahim Nash | 5215 |

akhayrutdinov | 5111 |

mb1973 | 4468 |

Quandray | 4444 |

Login to report an issue on this page.