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/Math / Number Theory

Technique

Computational Geometry Primitives

Use integer cross products for orientation, segment intersection, polygon area (shoelace), and point-in-polygon tests.

Learn firstMath / Number TheoryUnlocksConvex Hull Trick / Li Chao Tree
0%
mastery
0/7 solved
Practise nextMinimum Euclidean Distance
IdeaPracticePitfalls

Recognise it

The signal

points/segments/polygons

  1. 1Brute force

    Floating-point slopes and angles.

  2. 2The observation

    Precision errors and special cases: exact integer cross products remove division.

  3. 3Why it works

    sign(cross(b - a, c - a)) gives the orientation of (a, b, c).

Reach for it when

  • +collinearity, turn direction
  • +area or lattice points (Pick's theorem)

Not when

  • ×problem is actually Manhattan distance (rotate 45 degrees)

Practice

Next

Minimum Euclidean Distance

0/7 solved
  • Point Location TestCSESMedium
  • Line Segment IntersectionCSESMedium
  • Polygon AreaCSESMedium
  • Point in PolygonCSESMedium
  • Polygon Lattice PointsCSESMedium
  • Minimum Euclidean DistanceCSESHard
  • Nearest vectorsCodeforces2300

Pitfalls

Misconceptions

  • !using floating slopes (vertical lines, precision)
Go deepervariants and techniques · 2 combinations · resources

Variants and techniques

  • Convex Hull (Andrew's monotone chain)Sort points and build lower and upper hulls with a stack that pops non-left turns, in O(n log n).
  • Manhattan <-> Chebyshev RotationInterview · medium|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.

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