Given M sorted lists of variable length, efficiently compute the smallest range, including at least one element from each list.

For example,

Input:  4 sorted lists of variable length
 
[ 3, 6, 8, 10, 15 ]
[ 1, 5, 12 ]
[ 4, 8, 15, 16 ]
[ 2, 6 ]
 
Output:
 
The minimum range is 4–6
 
 
Input:  4 sorted lists of variable length
 
[ 2, 3, 4, 8, 10, 15 ]
[ 1, 5, 12 ]
[ 7, 8, 15, 16 ]
[ 3, 6 ]
 
Output:
 
The minimum range is 4–7

Practice this problem

We can solve this problem in O(N.log(M)) time using a min-heap where N is the total number of elements present in M lists. The idea is to construct a min-heap of size M and insert the first element of each list into it. Then pop the root element (minimum element) from the heap and insert the next element from the “same” list as the popped element. Repeat this process until any list is exhausted. To find the minimum range, maintain a variable high that stores the maximum element present in a heap at any point. Since the minimum element is present in the min-heap at its root, compute the range (high element – root element) and return the minimum range found at every pop operation.

The algorithm can be implemented as follows in C++, Java, and Python:

C++


Download  Run Code

Output:

The minimum range is (4, 6)

Java


Download  Run Code

Output:

The minimum range is (4, 6)

Python


Download  Run Code

Output:

The minimum range is (4, 6)

The time complexity of the above solution is O(N.log(M)) as the heap has size M, and we pop and push at most N times, where N is the total number of elements present in all lists. Note that each pop/push operation takes O(log(M)) time.