Given k sorted lists of integers of size n each, find the smallest range that includes at least element from each of the k lists. If more than one smallest ranges are found, print any one of them.
Input: K = 3 arr1 : [4, 7, 9, 12, 15] arr2 : [0, 8, 10, 14, 20] arr3 : [6, 12, 16, 30, 50] Output: The smallest range is [6 8] Explanation: Smallest range is formed by number 7 from first list, 8 from second list and 6 from third list. Input: k = 3 arr1 : [4, 7] arr2 : [1, 2] arr3 : [20, 40] The smallest range is [2 20]
Naive approach : The idea is to maintain pointers to every list using array ptr[k].Below are the steps :
- Initially the index of every list is 0,therefore initialize every element of ptr[0..k] to 0;
- Repeat the following steps until atleast one list exhausts :
- Now find the minimum and maximum value among the current elements of all the list pointed by the ptr[0…k] array.
- Now update the minrange if current (max-min) is less than minrange.
- increment the pointer pointing to current minimum element.
The smallest range is [6 8]
Time complexity : O(n2 k)
A Better efficient approach is to use min heap. Below are the steps –
- Create a min heap of size k and insert first elements of all k lists into the heap.
- Maintain two variables min and max to store minimum and maximum values present in the heap at any point. Note min will always contain value of the root of the heap.
- Repeat following steps
- Get minimum element from heap (minimum is always at root) and compute the range.
- Replace heap root with next element of the list from which the min element is extracted. After replacing the root, heapify the tree. Update max if next element is greater. If the list doesn’t have any more elements, break the loop.
Below is C++ implementation of above approach –
The smallest range is [6 8]
Time Complexity: The while loop inside findSmallestRange() function can run maximum n*k times. In every iteration of loop, we call heapify which takes O(Logk) time. Therefore, the time complexity is O(nk Logk).
This article is contributed by Aditya Goel. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
- Count number of smallest elements in given range
- Find the smallest and second smallest elements in an array
- Find common elements in three linked lists
- Find the smallest after deleting given elements
- Find smallest subarray that contains all elements in same order
- Find the k smallest numbers after deleting given elements
- Find missing elements of a range
- Find array elements with frequencies in range [l , r]
- Find a range that covers all the elements of given N ranges
- Find the lexicographically smallest sequence which can be formed by re-arranging elements of second array
- Find even occurring elements in an array of limited range
- Find duplicates in a given array when elements are not limited to a range
- Average of remaining elements after removing K largest and K smallest elements from array
- Sudo Placement[1.5] | Second Smallest in Range
- Elements to be added so that all elements of a range are present in array