Problem · Array
Minimum Absolute Pair Difference
Learn this problemProblem statement
Given an unordered integer array values, choose two elements at different indices and return the minimum possible absolute difference between their values.
Equal values at different indices are a valid pair and produce a minimum difference of 0.
Function
minimumDifference(values: int[]) → longExamples
Example 1
values = [1,20,100,3]return = 2The closest pair is 1 and 3, whose absolute difference is 2.
Example 2
values = [8,-4,12,-5]return = 1The pair -5 and -4 has difference 1.
Example 3
values = [7,2,7]return = 0The two occurrences of 7 are at different indices, so they form a valid pair with difference 0.
Constraints
2 <= values.length <= 200000-10^9 <= values[i] <= 10^9- The result fits in a signed 64-bit integer.