Tutorialspoint
Problem
Solution
Submissions

Smallest Range Covering Elements

Certification: Advanced Level Accuracy: 100% Submissions: 1 Points: 15

Write a Java program to find the smallest range that includes at least one number from each of k sorted lists. The range [a, b] is smaller than range [c, d] if (b - a) < (d - c), or if (b - a) == (d - c) and a < c. You have k sorted lists of integers, and you need to find the smallest range that includes at least one number from each of the k lists.

Example 1
  • Input: nums = [[4,10,15,24,26], [0,9,12,20], [5,18,22,30]]
  • Output: [20, 24]
  • Explanation: List 1: [4,10,15,24,26] List 2: [0,9,12,20] List 3: [5,18,22,30] The range [20,24] contains 24 from list 1, 20 from list 2, and 22 from list 3. No smaller range can be found that contains at least one element from each list.
Example 2
  • Input: nums = [[1,2,3], [1,2,3], [1,2,3]]
  • Output: [1, 1]
  • Explanation: Each list contains elements 1, 2, and 3. The range [1,1] contains the element 1 from each list. This is the smallest possible range as its size is 0.
Constraints
  • nums.length == k, where k is between 1 and 3500
  • 1 ≤ nums[i].length ≤ 50 for all 0 ≤ i < k
  • -10^5 ≤ nums[i][j] ≤ 10^5 for all 0 ≤ i < k and 0 ≤ j < nums[i].length
  • nums[i] is sorted in non-decreasing order
  • Time Complexity: O(n * log(k)) where n is the total number of elements across all lists
  • Space Complexity: O(k)
Priority QueueCapgeminiWalmart
Editorial

Login to view the detailed solution and explanation for this problem.

My Submissions
All Solutions
Lang Status Date Code
You do not have any submissions for this problem.
User Lang Status Date Code
No submissions found.

Please Login to continue
Solve Problems

 
 
 
Output Window

Don't have an account? Register

Solution Hints

  • Use a min-heap (priority queue) to track the smallest element from each list
  • Keep a current maximum value across all elements in the heap
  • Initialize the heap with the first element from each list
  • Repeatedly pop the smallest element, update the range if needed, and add the next element from the same list
  • Track the range with the minimum difference
  • Continue until any one of the lists is exhausted
  • The problem is similar to merging k sorted lists, but with a focus on finding the minimum range

Steps to solve by this approach:

 Step 1: Create a min-heap (priority queue) to track the smallest element from each list along with its list index and position.

 Step 2: Initialize the heap with the first element from each list, and keep track of the maximum value.
 Step 3: While the heap contains elements from all k lists, extract the minimum element from the heap.
 Step 4: Calculate the current range as (max - current min value) and update the smallest range if needed.
 Step 5: Add the next element from the same list as the extracted element to the heap, if available.
 Step 6: Update the maximum value if the newly added element is larger.
 Step 7: Continue until any list is exhausted, then return the smallest range found.

Submitted Code :