Technique · Interview · medium
Manhattan <-> Chebyshev Rotation
|x1-x2| + |y1-y2| = max(|u1-u2|, |v1-v2|) with u = x+y and v = x-y. So the max/min Manhattan distance reduces to max minus min of u and of v. In k dimensions, take the max over 2^(k-1) sign patterns.
0/6 solved
Recognise it
The signal
maximum Manhattan distance between points
1Brute force
All pairs: O(n2).
2The observation
The absolute values couple the coordinates.
3Why it works
For each sign pattern s, the max over pairs of s.(p_i - p_j) is max(s.p) - min(s.p).
Reach for it when
- +maximise |a_i - a_j| + |b_i - b_j| + |i - j|
- +remove one point to minimise the maximum Manhattan distance
- +k <= 5 dimensions with point updates
Not when
- ×the SUM of all pairwise distances (coordinates already separate: sort each axis + prefix sums)
- ×Euclidean distance
Practice
- Maximum of Absolute Value ExpressionMedium
- Minimize Manhattan DistancesHard
- Maximum Manhattan DistancesCSESHard
- Nearest Campsites IICSESHard
- Nearest Campsites ICSESHard
- Multidimensional QueriesCodeforces2300
Pitfalls
Misconceptions
- !using the rotation on sum-of-distances problems
Go deeper2 combinations · resources
Combinations
Manhattan <-> Chebyshev Rotation+Segment Tree / Fenwick (BIT)
max Manhattan distance among points in an index range, with updates, k <= 5
- Manhattan <-> Chebyshev Rotation:
- 2^(k-1) sign patterns turn L1 distance into max - min
- Segment Tree / Fenwick (BIT):
- range max and min per sign pattern, with point updates
Manhattan <-> Chebyshev Rotation+Ordered Set / Balanced BST (TreeMap, multiset)
remove exactly one point to minimise the max Manhattan distance
- Manhattan <-> Chebyshev Rotation:
- max distance = max(range of u, range of v)
- Ordered Set / Balanced BST (TreeMap, multiset):
- multisets of u and v support removing one point and reading the new extremes