#1200
Easy Algorithms Minimum absolute difference
Array Sorting
75.0% acceptance
Feb 25, 2026
2882
94
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, b are from arr
a < b
b - a equals to the minimum absolute difference of any two elements in arr
Solution
Rust
Time O(n log n)
Space O(n)
impl Solution {
pub fn minimum_abs_difference(arr: Vec<i32>) -> Vec<Vec<i32>> {
let mut arr = arr;
arr.sort();
let min_diff = arr.windows(2).map(|w| w[1] - w[0]).min().unwrap_or(0);
arr.windows(2)
.filter(|w| w[1] - w[0] == min_diff)
.map(|w| vec![w[0], w[1]])
.collect()
}
}