Problem Number: 1200 Difficulty: Easy Category: Array, Sorting LeetCode Link: https://leetcode.com/problems/minimum-absolute-difference/
Given an array of distinct integers arr, find all pairs of elements with the minimum absolute difference of any two elements.
Return a list of pairs in ascending order (with respect to pairs), each pair [a, b] follows
a, bare fromarra < bb - aequals the minimum absolute difference of any two elements inarr
Example 1:
Input: arr = [4,2,1,3]
Output: [[1,2],[2,3],[3,4]]
Explanation: The minimum absolute difference is 1. List all pairs with difference equal to 1 in ascending order.
Example 2:
Input: arr = [1,3,6,10,15]
Output: [[1,3]]
Explanation: The minimum absolute difference is 2.
Example 3:
Input: arr = [3,8,-10,23,19,-4,-14,27]
Output: [[-14,-10],[19,23],[23,27]]
Constraints:
2 <= arr.length <= 10^5-10^6 <= arr[i] <= 10^6
I used a Sorting approach to find the minimum absolute difference efficiently. The key insight is that the minimum absolute difference must occur between adjacent elements in the sorted array.
Algorithm:
- Sort the array in ascending order
- Initialize minimum absolute difference to infinity
- Iterate through adjacent pairs in the sorted array
- Calculate absolute difference between each pair
- If current difference is smaller than minimum, update minimum and reset result list
- If current difference equals minimum, add the pair to result list
- Return all pairs with minimum absolute difference
The solution uses sorting to find minimum absolute differences efficiently. See the implementation in the solution file.
Key Points:
- Sorts array to ensure adjacent elements have minimum differences
- Tracks minimum absolute difference found so far
- Collects all pairs with the minimum difference
- Returns pairs in ascending order as required
- Handles multiple pairs with same minimum difference
Time Complexity: O(n log n)
- Sorting: O(n log n)
- Single pass through sorted array: O(n)
- Total: O(n log n)
Space Complexity: O(n)
- Result list can contain up to n/2 pairs
- Each pair contains 2 elements
- Total: O(n)
-
Sorting Advantage: After sorting, the minimum absolute difference must occur between adjacent elements.
-
Adjacent Pairs: We only need to check consecutive elements in the sorted array, not all possible pairs.
-
Multiple Minimums: There can be multiple pairs with the same minimum absolute difference.
-
Efficient Tracking: We can track the minimum difference and collect all pairs with that difference in one pass.
-
Ordered Output: The sorted array naturally provides pairs in ascending order.
-
Distinct Elements: Since all elements are distinct, we don't need to handle duplicates.
-
Brute Force: Initially might check all possible pairs, leading to O(n²) complexity.
-
Wrong Order: Not sorting the array first, missing the adjacent pair insight.
-
Complex Logic: Overcomplicating the minimum tracking logic.
-
Missing Pairs: Not collecting all pairs with the same minimum difference.
- Two Sum (Problem 1): Find pairs that sum to target
- 3Sum (Problem 15): Find triplets that sum to zero
- Closest 3Sum (Problem 16): Find triplet closest to target
- Two Sum II (Problem 167): Two sum in sorted array
- Hash Set: Use hash set to track seen differences - O(n²) time complexity
- Two Pointers: Use two pointers on sorted array - O(n log n) time
- Priority Queue: Use min-heap to track differences - O(n log n) time
- Brute Force: Checking all possible pairs instead of using sorting.
- Wrong Order: Not sorting the array to find adjacent minimum differences.
- Missing Pairs: Not collecting all pairs with the same minimum difference.
- Complex Logic: Overcomplicating the minimum tracking.
- Order Issues: Not returning pairs in the required ascending order.
Note: This is a simple sorting problem that demonstrates efficient finding of minimum differences between adjacent elements.