FastPrepCount Minimum-Difference Pairs

Count Minimum-Difference Pairs

Shield AI logoShield AI● EasyFULLTIMEPHONE SCREEN
Learn

Problem statement

Given an array values of distinct integers, find the minimum absolute difference between any two values and return the number of unordered pairs that have that difference.

Function

countMinimumDifferencePairs(values: int[]) → int

Examples

Example 1

values = [4,2,1,3]return = 3

The minimum difference is 1, achieved by (1,2), (2,3), and (3,4).

Example 2

values = [1,5,9]return = 2

Constraints

  • 2 <= values.length <= 100000.
  • All values are distinct and fit in a signed 32-bit integer.
See Shield AI hiring insights
public int countMinimumDifferencePairs(int[] values) {
  // write your code here
}
values[4,2,1,3]
expected3
Checking account…