Sub topic 14
Minimising Sums of Absolute Values with the Median
This chapter assumes you can read |x − a| as the distance between x and a on the number line, and that you can split a multi-modulus expression at its critical points. Both were covered in earlier chapters. The aim here is to find the minimum of expressions like |x − 1| + |x − 4| + |x − 7| + |x − 10| in a few seconds, with no casework.
The Problem in Distance Language
Write the expression as
f(x) = |x − a₁| + |x − a₂| + … + |x − aₙ|, with a₁ ≤ a₂ ≤ … ≤ aₙ.
Each term is the distance from x to one fixed point. So f(x) is the total distance from x to all n points. CAT often dresses this up as a word problem: houses along a straight road, and the question of where to put a shop so the total walking distance is as small as possible.
The answer is the median of the points:
- If n is odd, the unique minimiser is the middle point.
- If n is even, every x between the two middle points, endpoints included, gives the same minimum.
Why the Median Works: The Slope Argument
Between any two neighbouring points, f is a straight line. Its slope depends on how many points lie on each side of x:
- Each point to the left of x adds +1 to the slope, because moving right takes you further from it.
- Each point to the right of x adds −1, because moving right brings you closer.
So the slope is (number of points left of x) − (number of points right of x).
When x is far to the left, every point is on the right and the slope is −n. Each time x passes a point, the slope goes up by 2. The function keeps falling while more points lie to the right than to the left. It starts rising once more points lie to the left. The turning point is where the two counts balance, and that is the median.
When n is even, there is a whole interval between the two middle points where the counts are exactly equal. There the slope is 0, so f is flat.
The flat bottom from 4 to 7 is the even-count case. The chart is symmetric here only because the points are evenly spaced. The flat bottom is there for any even set of points.
Computing the Minimum Value Fast
You don't need to substitute the median and add n terms. Pair the outermost points. For any x between a₁ and aₙ, |x − a₁| + |x − aₙ| = aₙ − a₁. The same holds for the next pair inward, and so on. At the median, x lies inside every pair at once, so
minimum = (aₙ − a₁) + (aₙ₋₁ − a₂) + (aₙ₋₂ − a₃) + …
When n is odd, the middle point pairs with itself and adds 0.
For the points 1, 4, 7, 10 the minimum is (10 − 1) + (7 − 4) = 12.
For |x − 1| + |x − 2| + … + |x − 10|, x can be anything in [5, 6], and the minimum is 9 + 7 + 5 + 3 + 1 = 25.
The brute-force check below confirms the 1, 4, 7, 10 case. The helper function after it applies the pairing rule to any list.
points = [1, 4, 7, 10]
best = min(sum(abs(x - a) for a in points) for x in range(-20, 21))
print(best) # 12, reached for any x between 4 and 7
def min_abs_sum(points):
"""Return (lo, hi, min_value): f is minimised for every x in [lo, hi]."""
s = sorted(points)
n = len(s)
lo, hi = s[(n - 1) // 2], s[n // 2]
value = sum(s[n - 1 - i] - s[i] for i in range(n // 2))
return lo, hi, value
print(min_abs_sum([1, 4, 7, 10])) # (4, 7, 12)
print(min_abs_sum(list(range(1, 11)))) # (5, 6, 25)
print(min_abs_sum([1, 2, 9])) # (2, 2, 8)Weighted Terms and Coefficients
Integer weights become repeated points
A term k|x − a| counts as k copies of the point a. Expand the list, then take the median as usual. This is called the weighted median.
Example: f(x) = |x − 1| + 2|x − 3| + 3|x − 8|. The expanded list is 1, 3, 3, 8, 8, 8. There are 6 entries, so the middle two are the 3rd and 4th entries, 3 and 8. The minimum holds on all of [3, 8], and its value is (8 − 1) + (8 − 3) + (8 − 3) = 17. Check: f(3) = 2 + 0 + 15 = 17 and f(8) = 7 + 10 + 0 = 17.
print(min_abs_sum([1, 3, 3, 8, 8, 8])) # (3, 8, 17)Coefficients on x must be factored out first
|2x − 1| is not a distance to 1. Rewrite it as 2|x − 1/2|. This is the most common trap.
Example: f(x) = |2x − 1| + |x − 3| = 2|x − 0.5| + |x − 3|. The expanded list is 0.5, 0.5, 3, so the median is 0.5. The minimum is f(0.5) = 0 + 2.5 = 2.5.
When one weight dominates
If one point carries more than half the total weight, the minimum is at that point, however far away the others are. In 5|x − 2| + |x − 10| + |x − 20|, the point 2 has weight 5 out of 7, so x = 2.
Edge Cases and Variations
| Situation | What happens | Example | Column | Column | Column | Column |
|---|---|---|---|---|---|---|
| Odd count | Unique minimiser at the middle point | 2, 5, 9 → x = 5, min 7 | ||||
| Even count | Flat minimum on [middle-left, middle-right] | 1, 4, 7, 10 → [4, 7] | ||||
| Repeated points | Count each repeat separately | 1, 3, 3 → x = 3 | ||||
| x restricted to integers | Count the integers in the flat interval | 1, 4, 7, 10 → 4 integers | ||||
| x restricted to an interval | f is convex, so take the point of the interval nearest the median | On [8, 12] for 1, 4, 7, 10 → x = 8 | ||||
| Asking for a maximum | Unbounded on all reals. On [p, q] the maximum is at p or q | See the exercises | ||||
| Two dimensions, sum of \ | x − aᵢ\ | + \ | y − bᵢ\ | The x-part and y-part separate. Take the median of each | Shop on a grid of streets |
Contrast with the Mean
If the expression is a sum of squared distances, Σ(x − aᵢ)², the minimiser is the mean of the points, not the median. Squares punish large gaps more heavily, so outlying points pull the answer toward them. With absolute values, moving an outer point further out changes the minimum value but not the location of the minimum. Check which kind of sum you have before you choose the method.
Exercise
Find the minimum value of |x − 2| + |x − 5| + |x − 9| and the value of x where it occurs.
Show answer
There are three points, so the median is 5. The minimum is (9 − 2) + 0 = 7, at x = 5.
Exercise
For how many integer values of x does |x + 3| + |x − 1| + |x − 4| + |x − 6| reach its minimum, and what is that minimum?
Show answer
The sorted points are −3, 1, 4, 6. The two middle points are 1 and 4, so every x in [1, 4] gives the minimum. That covers 4 integers (1, 2, 3, 4). The minimum is (6 − (−3)) + (4 − 1) = 9 + 3 = 12.
Exercise
Find the minimum of |3x − 6| + |x − 4|.
Show answer
Rewrite it as 3|x − 2| + |x − 4|. The expanded list is 2, 2, 2, 4, so the two middle entries are both 2 and the median is 2. The minimum is f(2) = 0 + 2 = 2.
Exercise
Find the maximum of |x − 1| + |x − 5| for x in [0, 8].
Show answer
The function is convex, so its maximum on a closed interval is at an endpoint. f(0) = 1 + 5 = 6 and f(8) = 7 + 3 = 10. The maximum is 10, at x = 8.
Exercise
Minimise |x − 1| + 2|x − 3| + 3|x − 8| + |x − 12|.
Show answer
The expanded list is 1, 3, 3, 8, 8, 8, 12, which has 7 entries. The 4th entry is 8, so the minimum is at x = 8. The minimum is (12 − 1) + (8 − 3) + (8 − 3) = 21. Check: f(8) = 7 + 10 + 0 + 4 = 21.