Skip to content
BASA DSA

Minimum Interval to Include Each Query

HardIntervals

For each query value q, return the length end-start+1 of the smallest closed interval containing q, or -1 if none contains it. Preserve query order.

Example 1

Input
intervals = [[1,4],[2,3],[6,8]]
queries = [3,5,7,3]
Output
[2,-1,3,2]

Explanation

3 uses [2,3] of length 2, 5 has no match, and 7 uses [6,8] of length 3; repeated 3 repeats its answer.

Example 2

Input
intervals = [[4,4]]
queries = [4,3]
Output
[1,-1]

Explanation

The singleton closed interval contains 4 with length one but excludes 3.

Example 3

Input
intervals = [[1,10],[2,5]]
queries = [1,5,10]
Output
[10,4,10]

Explanation

Only [1,10] covers 1 and 10; 5 uses shorter [2,5] of inclusive length 4.

Constraints

  • 1 <= number of intervals,queries <= 100000
  • Interval and query values are positive <= 10000000
  • Every interval has start <= end

Function contractminIntervalArguments: json, json
Loading draft…
SolutionRuns on your device
Loading draft…