Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
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 arra < bb - a equals to the minimum absolute difference of any two elements in arrExample 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]]
Example 3:
Input: arr = [3,8,-10,23,19,-4,-14,27] Output: [[-14,-10],[19,23],[23,27]]
Constraints:
2 <= arr.length <= 105-106 <= arr[i] <= 106Problem summary: 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
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array
[4,2,1,3]
[1,3,6,10,15]
[3,8,-10,23,19,-4,-14,27]
minimum-cost-of-buying-candies-with-discount)minimize-the-maximum-difference-of-pairs)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1200: Minimum Absolute Difference
class Solution {
public List<List<Integer>> minimumAbsDifference(int[] arr) {
Arrays.sort(arr);
int n = arr.length;
int mi = 1 << 30;
for (int i = 0; i < n - 1; ++i) {
mi = Math.min(mi, arr[i + 1] - arr[i]);
}
List<List<Integer>> ans = new ArrayList<>();
for (int i = 0; i < n - 1; ++i) {
if (arr[i + 1] - arr[i] == mi) {
ans.add(List.of(arr[i], arr[i + 1]));
}
}
return ans;
}
}
// Accepted solution for LeetCode #1200: Minimum Absolute Difference
func minimumAbsDifference(arr []int) (ans [][]int) {
sort.Ints(arr)
mi := 1 << 30
n := len(arr)
for i := 0; i < n-1; i++ {
if t := arr[i+1] - arr[i]; t < mi {
mi = t
}
}
for i := 0; i < n-1; i++ {
if arr[i+1]-arr[i] == mi {
ans = append(ans, []int{arr[i], arr[i+1]})
}
}
return
}
# Accepted solution for LeetCode #1200: Minimum Absolute Difference
class Solution:
def minimumAbsDifference(self, arr: List[int]) -> List[List[int]]:
arr.sort()
mi = min(b - a for a, b in pairwise(arr))
return [[a, b] for a, b in pairwise(arr) if b - a == mi]
// Accepted solution for LeetCode #1200: Minimum Absolute Difference
impl Solution {
pub fn minimum_abs_difference(mut arr: Vec<i32>) -> Vec<Vec<i32>> {
arr.sort();
let n = arr.len();
let mut mi = i32::MAX;
for i in 1..n {
mi = mi.min(arr[i] - arr[i - 1]);
}
let mut ans = Vec::new();
for i in 1..n {
if arr[i] - arr[i - 1] == mi {
ans.push(vec![arr[i - 1], arr[i]]);
}
}
ans
}
}
// Accepted solution for LeetCode #1200: Minimum Absolute Difference
function minimumAbsDifference(arr: number[]): number[][] {
arr.sort((a, b) => a - b);
let mi = 1 << 30;
const n = arr.length;
for (let i = 0; i < n - 1; ++i) {
mi = Math.min(mi, arr[i + 1] - arr[i]);
}
const ans: number[][] = [];
for (let i = 0; i < n - 1; ++i) {
if (arr[i + 1] - arr[i] === mi) {
ans.push([arr[i], arr[i + 1]]);
}
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.