A teacher keeps a seating-order roster of a class's most recent quiz scores: one integer percentage score per seat, with seats numbered starting at 0. Across several study-hall review sessions, the teacher repeatedly picks a contiguous block of seats and wants to know how close together the two nearest different scores are within that block — specifically, the smallest absolute difference between any two scores in the block that are not equal to each other. If every score in the chosen block happens to be identical, there is no such pair, and the answer for that block is -1.
n, the number of seats.n space-separated integers scores[0], scores[1], ..., scores[n-1], the quiz score recorded at each seat.q, the number of review-session queries.q lines contains two integers l r, describing one query: the inclusive seat range [l, r] to examine.Print q lines. On line i, print the minimum absolute difference between two distinct scores among scores[l..r] for the i-th query, or -1 if every score in that range is identical.
Example 1
Input
6 5 3 5 8 8 2 2 0 3 2 5
Expected
2 3
Explanation
With scores = [5, 3, 5, 8, 8, 2], the first query covers seats 0-3 -> values {5, 3, 5, 8}. The distinct values present are 3, 5, and 8; the closest pair is 3 and 5, giving a difference of 2. The second query covers seats 2-5 -> values {5, 8, 8, 2}. The distinct values present are 2, 5, and 8; every adjacent gap (5-2=3 and 8-5=3) equals 3, so the answer is 3.
Example 2
Input
5 6 6 6 9 6 1 0 1
Expected
-1
Explanation
With scores = [6, 6, 6, 9, 6], the single query covers seats 0-1 -> values {6, 6}. Both scores in this range are identical, so there is no pair of distinct scores to compare, and the answer is -1.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →