Minimum Sum of Absolute Differences of Pairs is a clean greedy problem. You are handed two equal-length arrays and asked to pair every element of one with an element of the other. The twist is choosing the pairing that makes the grand total as small as possible.
Problem. Given two integer arrays a and b of the same length, pair each element of a with a
distinct element of b. For a pairing, the cost is the sum of |a[i] - b[i]| over all pairs. Return the
minimum possible cost.
Example: a = [5, 1, 3], b = [4, 8, 2] → answer 5 (pair as 1↔2, 3↔4, 5↔8: 1 + 1 + 3 = 5).
The slow way first
There are n! ways to pair the two arrays. Trying them all and keeping the smallest total is correct but factorial — hopeless past a handful of elements. Even being clever with permutations, brute force explodes fast.
The question to ask: is there an arrangement I can argue is always best, without searching? If so, I skip the search entirely.
The idea: sort both, then pair in order
Sort both arrays ascending and pair them position-for-position: smallest with smallest, next with next, largest with largest. This is the greedy choice, and it is provably optimal.
Why does sorting win? If two pairs are ever crossed — a smaller value matched to a larger partner while a larger value takes the smaller partner — uncrossing them (matching small with small) never increases the total and often shrinks it. So the fully sorted, uncrossed arrangement is at the minimum.
Walk through it
Step through the animation. Both rows start unsorted, then snap into ascending order. The pointer sweeps column by column: each column shows |a[i] - b[i]|, and the running total accumulates underneath. By the last column the total is the answer — no search, just one pass after the sort.
Pseudocode
sort a ascending
sort b ascending
total = 0
for each position i:
total = total + absolute_value(a[i] - b[i])
return totalThe Python solution
def min_sum_abs_diff(a, b):
a.sort()
b.sort()
total = 0
for x, y in zip(a, b):
total += abs(x - y)
return totala.sort()andb.sort()put both arrays in ascending order — the heart of the greedy choice.totalaccumulates the cost as we go.zip(a, b)walks both sorted rows together, handing us one aligned pair(x, y)at a time.total += abs(x - y)adds the absolute difference of the current column.- We return
totalonce every column is paired.
Complexity
| Case | Time | Notes |
|---|---|---|
| Brute force (all pairings) | O(n!) (slow) | try every permutation |
| Sort then pair (this solution) | O(n log n) (moderate) | sorting dominates the linear sweep |
O(1) (fast)Sorting costs O(n log n); the pairing sweep is a single O(n) pass. We need no extra structures beyond the sort, so the auxiliary space is O(1) (ignoring the sort's own bookkeeping).
When this pattern shows up
When a problem asks you to pair, match, or align two sequences to minimize (or maximize) a total of per-pair costs, try sorting both and matching in order first. Sorted-and-aligned is the optimal arrangement for a surprising number of these — assignment, scheduling, and difference-minimizing problems.
This greedy only holds because the cost is the absolute difference of paired values. If pairs interact (e.g. the cost depends on more than one column at once), sorting is no longer guaranteed optimal and you may need a real matching algorithm.
Practice
For a = [5, 1, 3] and b = [4, 8, 2], after sorting both, which value of a pairs with the 8 in b, and what does that pair contribute?
1. What is the greedy choice that makes this O(n log n)?
2. Why does uncrossing a crossed pair never increase the total?
3. What dominates the running time?
4. When does this sort-and-pair greedy stop being guaranteed optimal?