A warehouse scale logs the recorded weight, in grams, of every bin that rolls across it, in the order the bins were weighed. The inventory system wants to know how close together the two nearest readings are — bins that are not adjacent in the log still count for this comparison, and it is fine, even expected, for two different bins to have been recorded with the exact same weight.
Given the log of n recorded weights, find the minimum absolute difference between the recorded weights of any two distinct bins in the log (any two different positions, regardless of how far apart they were weighed).
Line 1: an integer n.
Line 2: n integers w_1 w_2 ... w_n, the recorded weights in the order the bins were weighed.
A single integer: the minimum absolute difference between the weights of any two distinct bins.
Example 1
Input
4 3 8 1 9
Expected
1
Explanation
The recorded weights sorted are 1, 3, 8, 9. The closest pair is 8 and 9, differing by 1, which is smaller than every other pair's gap (for example 3 and 1 differ by 2, and 9 and 3 differ by 6), so the answer is 1.
Example 2
Input
2 5 5
Expected
0
Explanation
There is only one pair of bins, and both were recorded at 5 grams, so their absolute difference is 0.
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 →