1200. Minimum Absolute Difference
Asked at Walmart
Difficulty
Easy
Time
O(n log n)
Space
O(1)
Pattern
Array
Companies
1
Problem
You are given an array arr of distinct integers. Look at every possible pair of elements and find the smallest absolute difference between any two of them. Call this value minDiff.
Return all pairs [a, b] that reach this difference, where a < b. The list of pairs must be sorted in ascending order: compare pairs by their first element, then by their second element.
A key observation: after you sort the array, the closest two values are always next to each other. So you never need to check far-apart elements — only neighbors.
Find the smallest gap, then list every pair with it
Constraints
- ●
2 <= arr.length <= 10^5 - ●
-10^6 <= arr[i] <= 10^6 - ●
All elements of arr are distinct
Examples
arr = [4,2,1,3][[1,2],[2,3],[3,4]]arr = [1,3,6,10,15][[1,3]]arr = [3,8,-10,23,19,-4,-14,27][[-14,-10],[19,23],[23,27]]Asked At
1 companies| Company | Difficulty | |
|---|---|---|
| Walmart | Easy | View all Walmart questions → |
How to Think About It
Brute force: compare every pair of elements, track the minimum absolute difference.
Sort the array first so that the minimum difference must be between adjacent elements.
After sorting, make one pass computing arr[i+1] - arr[i] to find the minimum diff.
Collect all pairs that achieve the minimum difference in a second pass.
Time O(n log n) for sorting, space O(1) excluding the output. Total time O(n log n).
Optimal Approach
Sort the array. Initialize minDiff to a large value. Iterate through adjacent pairs, updating minDiff whenever a smaller difference is found. In a second pass, collect all pairs whose difference equals minDiff. Sorting guarantees the minimum difference is between neighbors. Time complexity is O(n log n) due to sorting. Space is O(1) auxiliary.
Sort, scan for minDiff, scan again to collect
What Trips People Up in Real Interviews
Confirm that all elements are distinct (simplifies the logic).
Ask whether the output order matters (ascending order of pairs).
Mention that after sorting, non-adjacent pairs cannot have a smaller difference.
Edge cases: array of size 2 (return that single pair), all pairs have same difference.
Use a list of pairs rather than a set to maintain sorted output order.
Solution Code
O(n log n) timedef minimumAbsDifference(arr):
arr.sort()
min_diff = float('inf')
for i in range(len(arr) - 1):
min_diff = min(min_diff, arr[i + 1] - arr[i])
result = []
for i in range(len(arr) - 1):
if arr[i + 1] - arr[i] == min_diff:
result.append([arr[i], arr[i + 1]])
return resultFrequently Asked Questions
What is the Minimum Absolute Difference problem?
Given an array of distinct integers arr, find all pairs of elements with the minimum absolute difference in ascending order. Sorting the array ensures that the minimum difference always occurs between adjacent elements, reducing the problem to a single pass.
How do you solve Minimum Absolute Difference?
The optimal approach runs in O(n log n) time and O(1) space. Scroll up to "Optimal Approach" for the step-by-step walkthrough, diagrams, and code in Python, C++ and Java.
What companies ask Minimum Absolute Difference?
Minimum Absolute Difference is asked at Walmart. It is a easy difficulty problem.
What are common mistakes on Minimum Absolute Difference?
- Confirm that all elements are distinct (simplifies the logic).
- Ask whether the output order matters (ascending order of pairs).
- Mention that after sorting, non-adjacent pairs cannot have a smaller difference.
- Edge cases: array of size 2 (return that single pair), all pairs have same difference.
- Use a list of pairs rather than a set to maintain sorted output order.