Minimum Absolute Difference

EasyArrayBinary SearchSorting

Description

Given an array of distinct integers arr, find all pairs of elements with the minimum absolute difference. Return the pairs in ascending order.

Examples

Input:arr = [4,2,1,3]
Output:[[1,2],[2,3],[3,4]]
Explanation:

All have diff 1.

Input:arr = [5,5,5,5]
Output:[[5,5],[5,5],[5,5]]
Explanation:

When all elements are identical, the minimum absolute difference is 0. All adjacent pairs in the sorted array have difference 0.

Input:arr = [10,6,15,2]
Output:[[2,6],[6,10]]
Explanation:

After sorting [2,6,10,15], the differences are: |6-2|=4, |10-6|=4, |15-10|=5. The minimum difference is 4, so the pairs with difference 4 are returned.

Constraints

  • 2 ≤ arr.length ≤ 10⁵

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!