Maximum Points in a Rotating Angle

Coding · Medium · Free problem

You are given a set of $n$ points in the 2D plane and a fixed angle $\alpha$ (in radians). An "angular wedge" of width $\alpha$ is centered at the origin and can be rotated freely. Find the rotation that maximizes the number of points contained within the wedge.

Constraints: - $1 \le n \le 10^5$ - $0 < \alpha < 2\pi$ - Points may lie anywhere in the plane (excluding the origin) - Points on the boundary of the wedge count as contained

Examples:

Example 1: points = [(1,0), (0,1), (-1,0), (0,-1)], alpha = pi/2 Output: 2 -- a quarter-circle wedge can contain at most 2 of the 4 axis-aligned points.

Example 2: points = [(1,1), (1,2), (2,1)], alpha = pi/2 Output: 3 -- rotate the wedge to face roughly 45 degrees and all 3 points fit inside.

Hints

  1. Convert all points to polar angles relative to the origin -- the problem reduces to finding the largest number of angles that fit within a window of size $\alpha$ on a circle.
  2. Sort the angles and use a two-pointer sliding window of width $\alpha$.
  3. Duplicate the sorted angle array by appending each angle $+ 2\pi$; this makes wrap-around at $\pm\pi$ transparent to the sliding window.

Worked Solution

How to Think About It: The wedge is a pie slice of fixed angular width $\alpha$; only each point's *angle* from the origin matters, never its distance. Rotating the wedge = sliding a window of angular width $\alpha$ around the circle and counting how many angles fall inside. The key extremal fact (the "push-to-the-boundary" heuristic): the optimal window can always be slid until its trailing edge sits exactly on some point. So you never need to test infinitely many rotations — only the $n$ rotations that start each window at an existing point's angle. That turns a continuous optimization into a sorted-array sliding window. The one trap is the wrap-around at $\pm\pi$: a window can straddle the seam, which a naive linear scan misses.

Quick Estimate: Check the logic on Example 2, [(1,1),(1,2),(2,1)], $\alpha = \pi/2 = 90^\circ$, by eye. Angles: $(1,1)$ is $45^\circ$; $(2,1) = \arctan(1/2) \approx 27^\circ$; $(1,2) = \arctan 2 \approx 63^\circ$. All three sit inside $[27^\circ, 63^\circ]$, a spread of only $36^\circ < 90^\circ$, so a single $90^\circ$ wedge aimed near $45^\circ$ swallows all three — answer $3$, matching. For Example 1 the four axis points are $90^\circ$ apart; a *closed* $90^\circ$ wedge catches two adjacent ones (e.g. $0^\circ$ and $90^\circ$ on its boundaries) — answer $2$. Both fall straight out of "how many angles fit in a $\alpha$-wide arc."

Approach: Polar-sort angles, duplicate with $+2\pi$, two-pointer window of width $\alpha$.

Formal Solution:

  1. Map each point to $\theta_i = \text{atan2}(y,x) \in (-\pi,\pi]$.
  2. Sort the $n$ angles ascending.
  3. Un-wrap the circle: append $\theta_i + 2\pi$ for every angle, yielding a sorted length-$2n$ array. A window that crosses the $\pm\pi$ seam now appears as a contiguous run in this doubled array, so no modular arithmetic is needed in the loop.
  4. Two-pointer sweep: for each left index $i \in [0,n)$, advance a right pointer $j$ while $\theta_j - \theta_i \le \alpha$. The count $j - i$ is how many points a wedge whose trailing edge is at $\theta_i$ contains. Track the max.

```python import math

def max_points_in_angle(points, alpha): angles = sorted(math.atan2(y, x) for x, y in points) n = len(angles) angles = angles + [a + 2 * math.pi for a in angles] # un-wrap max_count = 0 j = 0 for i in range(n): if j < i: j = i while j < 2 * n and angles[j] - angles[i] <= alpha: j += 1 max_count = max(max_count, j - i) return max_count ```

Each pointer only moves forward across $2n$ entries, so the sweep is $O(n)$; the sort dominates at $O(n\log n)$.

Answer: $O(n\log n)$ via angular sort + $+2\pi$ un-wrapping + width-$\alpha$ sliding window; the max window size is the answer. Examples give $2$ and $3$.

Intuition

The reduction from geometry to circular interval scheduling is the key move. Once you see that "rotating the wedge" is equivalent to "sliding a window of size $\alpha$ over a circle of angles," the algorithmic pattern is immediate: sort, then two pointers. This is a recurring reduction in computational geometry -- optimal placement problems often reduce to sorting by angle and doing a linear scan.

The duplication trick (appending $\theta_i + 2\pi$) is a standard way to handle circular data structures without messy modular arithmetic in inner loops. It appears in problems like "maximum arc covered by $k$ points on a circle" and "largest circular subarray sum." In quant contexts, a similar idea shows up in calendar-effect analysis where you want to find the contiguous window of days/months with maximum cumulative signal, and the calendar wraps around.

Open the full interactive solver →