Advertisement

Minimum Absolute Difference - LeetCode 1200 Solution

Minimum Absolute Difference - Complete Solution Guide

Minimum Absolute Difference is LeetCode problem 1200, a Easy level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

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 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

Detailed Explanation

This problem challenges us to pinpoint the absolute smallest difference possible between any two distinct numbers within a given array, `arr`. Once that global minimum difference is identified, our task isn't just to report *a* pair exhibiting it, but to meticulously collect *all* such pairs. Each pair `[a, b]` must satisfy `a < b`, and their difference `b - a` must precisely equal this minimum. The final output is expected as a list of these pairs, ordered ascendingly based on the pairs themselves (e.g., `[1,2]` before `[2,3]`).

Solution Approach

The provided solution leverages a fundamental property of sorted data to dramatically simplify the problem. Its first, crucial step is to sort the input array `arr`. This is a game-changer because, in a sorted sequence, the smallest difference between any two distinct numbers *must* always occur between adjacent elements. Consider three distinct numbers `x < y < z`; it's clear that `z - x` will always be greater than both `y - x` and `z - y`. This insight eliminates the need for an expensive `O(N^2)` brute-force comparison of every possible pair.

Step-by-Step Algorithm

  1. Sort the input array `arr` in ascending order.
  2. Initialize `min_diff` to a large value (infinity in Python, Integer.MAX_VALUE in Java, INT_MAX in C++). Initialize an empty result list `result`.
  3. Iterate through the sorted array, starting from the second element. For each element, calculate the difference with the preceding element: `diff = arr[i] - arr[i-1]`.
  4. If `diff` is less than `min_diff`, update `min_diff` to `diff`, and reset `result` to contain only the current pair `[arr[i-1], arr[i]]`.
  5. If `diff` is equal to `min_diff`, append the current pair `[arr[i-1], arr[i]]` to `result`.
  6. After iterating through the entire array, return the `result` list.

Key Insights

  • **Sorting as a Prerequisite for Localized Search:** Sorting the input array `arr` is the foundational step. It transforms the problem from a potentially quadratic search space (any `arr[i]` vs. any `arr[j]`) into a linear one, where we only need to examine adjacent elements. This pre-processing makes the subsequent comparison phase incredibly efficient.
  • **Minimum Difference Resides Between Neighbors:** A key mathematical insight here is that for distinct numbers in a sorted array, the minimum absolute difference between any two elements will always be found by comparing an element `arr[i]` with its immediate predecessor `arr[i-1]`. This allows us to sweep through the array once, checking only `N-1` differences, rather than `N*(N-1)/2` differences, after sorting.
  • **Single-Pass Dynamic Collection:** The solution efficiently combines finding the minimum difference with collecting all corresponding pairs in a single pass. It doesn't require a separate second pass after determining the `min_diff`. Instead, it maintains `min_diff` dynamically; if a smaller difference is found, the `result` list is reset. If an equal difference is found, the pair is simply appended. This strategy is elegant, avoids redundant work, and naturally produces the pairs in the required ascending order due to the initial sort.

Complexity Analysis

Time Complexity: O(n log n)

Space Complexity: O(n)

Topics

This problem involves: Array, Sorting.

Companies

Asked at: Agoda, Audible, IBM, J.P. Morgan, PayPal, Paycom.