Problem · Array

Minimum Difference

Learn this problem
EasyIBMFULLTIMEOA
See IBM hiring insights

Problem statement

Given a set of distinct measurements taken at different times, find the minimum possible difference between any two measurements. Print all pairs of measurements that have this minimum difference in ascending order, with the pairs' elements ordered ascending. e.g., if a < b, the pair is a b. The values should have a single space between them.

Function

ibmMinimumDifference2(measurements: int[]) → int[][]

Complete the function minimumDifference in the editor.

minimumDifference has the following parameter:

  • int measurements[n]: an array of integers

Returns

NONE

(NOTE: NONE is specified by the orignal source. BUT, we wouldnt be able to upload the question if it returns nothing, so we made it return a 2D int arr instead :) 🥑 By spike, Dec 2024 🍉

Prints

Print the distinct pairs of measurements that have the minimum absolute difference, displayed in ascending order, with each pair separated by one space on a single line

🦥 Thank spike for the 1011st time! 💝

Examples

Example 1

measurements = [-1, 3, 6, -5, 0]return = [[-1, 0]]

🍓 Source note: Corrected on 2026-07-17. The source overview overlooked the adjacent values -1 and 0.

After sorting, the measurements are [-5, -1, 0, 3, 6]. The smallest adjacent difference is 0 - (-1) = 1, so the only pair with the minimum absolute difference is [-1, 0].

Example 2

measurements = [6, 5, 4, 3, 7]return = [[3, 4],[4, 5],[5, 6],[6, 7]]
The minimum absolute difference is 1, and the pairs with that difference are (3,4), (4,5), (5,6), and (6,7).

Example 3

measurements = [1, 3, 5, 10]return = [[1, 3], [3, 5]]
The minimum absolute difference between any two elements in the array is 2, and there are two such pairs: (1, 3) and (3, 5).

Constraints

  • 2 ≤ n ≤ 10^5
  • -10^9 ≤ measurements[i] ≤ 10^9

More IBM problems

drafts saved locally
public int[][] ibmMinimumDifference2(int[] measurements) {
  // write your code here
}
measurements[-1, 3, 6, -5, 0]
expected[[-1, 0]]
checking account