Finding out the minimum difference between elements in an array
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Finding the minimum difference between elements in an array is a common task in interviews and real systems such as ranking, scheduling, and threshold analysis. The straightforward brute-force solution is easy to write but slow for large inputs. The standard efficient approach is sorting followed by adjacent comparison.
Problem Definition
Given an integer array, find the smallest absolute difference between any two distinct elements.
Example:
- input:
[4, 9, 1, 32, 13] - answer:
3, from pairs(1, 4)or(9, 13)
For arrays with fewer than two elements, no valid pair exists. Your function should raise an error or return a sentinel value according to API design.
Brute-Force Baseline
Check every pair and track the best difference.
Complexity is O(n^2), which becomes expensive as n grows.
Sorting-Based Efficient Solution
After sorting, the smallest absolute difference must occur between adjacent values in sorted order.
Complexity is O(n log n) due to sorting, with linear scan after.
Why Adjacent Comparison Works
Assume sorted values a <= b <= c. If you compare non-adjacent a and c, then c - a is at least as large as either b - a or c - b. So a non-adjacent pair cannot produce a strictly smaller difference than all adjacent pairs.
That proof is why adjacent scan is both correct and efficient.
Return the Pair Along with the Difference
Many applications need not only the value but also which pair produced it.
Returning the pair improves explainability and debugging.
Handle Duplicates and Negative Numbers
The sorting approach naturally supports duplicates and negatives.
Duplicate values yield minimum possible difference 0.
In-Place Versus Copy Sorting
sorted(arr) returns a copy, while arr.sort() mutates input. Choose based on caller expectations.
In-place variant:
Document mutation behavior clearly to avoid surprises.
C++ Version
The same logic applies in C++.
This keeps the same O(n log n) complexity profile.
Common Pitfalls
A common pitfall is forgetting to sort before adjacent comparison, which invalidates the logic.
Another issue is using absolute difference after sorting when simple subtraction already yields non-negative values and is slightly cleaner.
Some implementations forget edge cases with fewer than two elements and crash on index access.
In-place sorting without documentation is another frequent bug source when callers expect original order preserved.
Finally, brute-force solutions are sometimes used in production for convenience and later become bottlenecks at scale.
Summary
- Brute force is simple but slow at
O(n^2). - Sorting plus adjacent scan solves the problem in
O(n log n). - Adjacent comparison works because non-adjacent gaps cannot be smaller.
- Duplicates and negatives are naturally handled.
- Decide and document whether your function mutates input.
Related reading
- Finding out whether there exist two identical substrings one next to another
- Finding pairs with product greater than sum
- Finding positions of milestones given their pairwise distances
- Finding reachable vertices for every vertex in a directed graph
- Finding overlapping data in arrays
- Finding patterns in list
- Finding set of pairs that correspond to list of sums
- Finding shortest repeating cycle in word?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.