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 these rules:
a
and 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 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 <= 10^5
-10^6 <= arr[i] <= 10^6
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force method to find the smallest difference between any two numbers involves checking every single pair. It's like comparing each number in a group to every other number to see which pair is closest together. This ensures that we explore all the options to find the absolute minimum difference.
Here's how the algorithm would work step-by-step:
def minimum_absolute_difference_brute_force(numbers):
smallest_difference = float('inf')
# Iterate through each number in the list.
for first_number_index in range(len(numbers)):
# Compare with remaining numbers.
for second_number_index in range(first_number_index + 1, len(numbers)):
# Compute absolute difference.
current_difference = abs(numbers[first_number_index] - numbers[second_number_index])
# Update if smaller difference is found.
if current_difference < smallest_difference:
smallest_difference = current_difference
return smallest_difference
To find the smallest difference, we can't check every single pair. Instead, we first put the numbers in order. Then, because the smallest difference must be between numbers that are next to each other, we only need to check those pairs.
Here's how the algorithm would work step-by-step:
def minimum_absolute_difference(numbers):
numbers.sort()
# Initialize min_diff to a large value.
minimum_difference = float('inf')
for i in range(len(numbers) - 1):
# The minimum difference must be between
# adjacent elements after sorting.
current_difference = abs(numbers[i] - numbers[i + 1])
# Track the smallest difference.
if current_difference < minimum_difference:
minimum_difference = current_difference
return minimum_difference
Case | How to Handle |
---|---|
Empty or null input array | Return a predefined value like Integer.MAX_VALUE or throw an exception, indicating no minimum difference can be calculated. |
Input array with only one element | Return a predefined value like Integer.MAX_VALUE or throw an exception, since a minimum difference requires at least two elements. |
Input array with only two elements | Calculate and return the absolute difference between the two elements directly. |
Input array contains duplicate values | The algorithm should correctly calculate the minimum difference even if the array contains duplicates. |
Input array contains negative numbers | The absolute difference calculation should handle negative numbers correctly. |
Input array contains large numbers, potentially leading to integer overflow | Use `long` data type to store the absolute difference to prevent integer overflow or use subtraction method that doesn't cause overflow. |
Input array contains all identical elements | The minimum absolute difference will be 0 in this case; the algorithm should correctly identify this. |
Large input array size to test for time complexity | Ensure the sorting approach used is efficient (e.g., O(n log n)) and avoids unnecessary calculations. |