Skip to content
  • Dashboard
  • Review
  • Go: plan & chat
  • Roadmap
  • Patterns & lessons49
  • Sheets
  • Problems1.5k
  • Ascend
  • Toolkit
  • Core CS + SQL
  • System Design
  • Machine Coding
  • Interview hub
  • Mock interview
  • Companies & OA
  • Leaderboard
  • Battles
  • College Cup
  • People

Keep your progress

Sync across devices with a free account.

Sign upSign in
Patterns & lessons
Sign inSign up
Patterns/Math & Number Theory/Computational Geometry Primitives

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.

Learn firstComputational Geometry PrimitivesMath / Number Theory
0%
mastery
0/6 solved
Practise nextMaximum of Absolute Value Expression
IdeaPracticePitfalls

Recognise it

The signal

maximum Manhattan distance between points

  1. 1Brute force

    All pairs: O(n2).

  2. 2The observation

    The absolute values couple the coordinates.

  3. 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

Next

Maximum of Absolute Value Expression

0/6 solved
  • 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

    Multidimensional Queries

  • 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

    Minimize Manhattan Distances

Resources

  • Binary exponentiationcp-algorithms · article
  • Sieve of Eratosthenescp-algorithms · article
  • Modular inversecp-algorithms · article
  • DivisibilityUSACO Guide · course
  • CombinatoricsUSACO Guide · course
  • Math cheatsheetTech Interview Handbook · article
TodayReviewPatternsProblems