Sorting · Easy
A diagnostics report contains unsorted integer measurements. It needs the smallest difference between any two observations. The same numeric measurement at two different positions is a valid pair with zero difference.
Return the minimum absolute difference between values at two distinct input positions.
Input
Call
- Implement solve(input) and return the result. The provided adapter handles JSON input and output.
Input shape
{ nums: integer[] }
Fields
- nums is an array of signed integers. Repeated values at different positions are separate entries.
Output
Return value
- Return the gap, not the pair or its positions. Return null with fewer than two observations.
- Equal measurements at different positions produce zero. Input order must remain unchanged.
Constraints
- 0 <= nums.length <= 100000; -1000000000 <= nums[i] <= 1000000000.
- All totals and counts fit exactly in a safe JSON integer.
Examples
Example 1
{
"nums": [
9,
1,
5,
8
]
}Returns
1Example 2
{
"nums": [
4,
4
]
}Returns
0Required complexity
O(n log(n + 1)) time and O(n) copy space