Smallest Number Range (Hard)
Problem Statement
Given ‘M’ sorted arrays, find the smallest range that includes at least one number from each of the ‘M’ lists.
Example 1:
Input: L1=[1, 5, 8], L2=[4, 12], L3=[7, 8, 10]
Output: [4, 7]
Explanation: The range [4, 7] includes 5 from L1, 4 from L2 and 7 from L3.
Example 2:
Input: L1=[1, 9], L2=[4, 12], L3=[7, 10, 16]
Output: [9, 12]
Explanation: The range [9, 12] includes 9 from L1, 12 from L2 and 10 from L3.
Try it yourself
Try solving this question here:
import java.util.*;class SmallestRange {public static int[] findSmallestRange(List<Integer[]> lists) {// TODO: Write your code herereturn new int[] { -1, -1 };}public static void main(String[] args) {Integer[] l1 = new Integer[] { 1, 5, 8 };Integer[] l2 = new Integer[] { 4, 12 };Integer[] l3 = new Integer[] { 7, 8, 10 };List<Integer[]> lists = new ArrayList<Integer[]>();lists.add(l1);lists.add(l2);lists.add(l3);int[] result = SmallestRange.findSmallestRange(lists);System.out.print("Smallest range is: [" + result[0] + ", " + result[1] + "]");}}
Solution
This problem follows the K-way merge pattern and we can follow a similar approach as ...