educative.io

Smallest Number Range (Hard)

I am confused about: Smallest Number Range (Hard) - Grokking the Coding Interview: Patterns for Coding Questions

"Given ‘M’ sorted arrays, find the smallest range that includes at least one number from each of the ‘M’ lists.

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.
```"
I am confused why the range is [4,7]. Why not [5,7] (5 from L1 7 from L3 still within the range in L2) or even [8,8] as those also are within the ranges of all three arrays. 

What am I missing? 



----
Course: https://www.educative.io/courses/grokking-the-coding-interview
Lesson: https://www.educative.io/courses/grokking-the-coding-interview/JPGWDNRx3w2

Hi @Michal2!
Thanks for reaching out to us. In this problem, you are supposed to find the smallest range that includes atleast one element from each list. Here
Input: L1=[1, 5, 8], L2=[4, 12], L3=[7, 8, 10]
Output: [4,7]
Explanation:
L1: [1,5,8], 5 is in range [4,7].
L2: [4,12], 4 is in range [4,7].
L3: [7,8,10], 7 is in range [4,7].
We cannot select range [5,7] as there is not even one number from list 2 fall in this range. L2 numbers either 4 or 12 should be in the range. 4 and 12 from list 2 are the numbers, not the range here. We need range as output
Similarly range [8,8] cannot be selected because list 2 numbers do not fall in this range neither 4 nor 12.
I hope it helps

Hi :slight_smile:
Thank you! My mistake was to treat the input as if it were range(s) as well.
Now it make sense to me.

Thanks again!

1 Like