FastPrepCount Minimum-Difference Pairs
Problems/Count Minimum-Difference Pairs

Count Minimum-Difference Pairs

Shield AI logoShield AI● EasyFULLTIMEPHONE SCREEN
QuestionFunctionExamplesConstraints

Problem statement

Source note: A first-hand Shield AI coding interview asks for the count of minimum-difference pairs. The distinct-input convention is a bounded practice assumption; core match is about 85%.

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
Saved
public int countMinimumDifferencePairs(int[] values) {
  // write your code here
}
Checking account…