Problem · Array

Minimum Absolute Pair Difference

Learn this problem
EasyUpstart logoUpstartFULLTIMEOA

Problem 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[]) → long

Examples

Example 1

values = [1,20,100,3]return = 2

The closest pair is 1 and 3, whose absolute difference is 2.

Example 2

values = [8,-4,12,-5]return = 1

The pair -5 and -4 has difference 1.

Example 3

values = [7,2,7]return = 0

The 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.

More Upstart problems

drafts saved locally
public long minimumDifference(int[] values) {
    // Write your code here.
}
values[1,20,100,3]
expected2
checking account