Skip to problem
←

Maximum Number of Visible Points

Medium 0 attempts

From a fixed position location on a 2D plane, you may rotate your viewing direction freely but cannot move. Your field of view is a sector of width angle degrees. Return the maximum number of input points visible at the same time.

Points on either boundary of the sector count as visible. Distance does not limit visibility, and points do not block one another. Duplicate coordinates represent separate points and each counts. Points exactly at location are always visible, regardless of direction.

An angle of 360 sees every point. For narrower angles, the sector can cross any compass direction, including the boundary where angle measurements wrap around.

Function contract

Implement visiblePoints(points, angle, location). Receive [x, y] point pairs, a field-of-view angle in degrees, and your fixed [x, y] location.

Output: Return the maximum number of points visible simultaneously over all viewing directions.

Example 1

Input
points = [[2,1],[2,2],[3,3]]
angle = 90
location = [1,1]
Output
3

Example 2

Input
points = [[1,0],[2,1]]
angle = 13
location = [1,1]
Output
1

Example 3

Input
points = [[1,0],[0,1],[-1,0]]
angle = 360
location = [0,0]
Output
3
Visual Aid
Field-of-view boundaries are inclusive
A ninety-degree field of view includes both boundary pointsObserver at origin. Points lie east, north, west, and south. A ninety-degree sector between east and north includes two points.90°(0,1)(1,0)(−1,0)(0,−1)observerBoth edges count.2 visible pointsReturn 2

With four points in the cardinal directions and angle = 90°, the best view contains two points, including points exactly on its edges.

Constraints
  • 1 <= points.length <= 10^5
  • 1 <= angle <= 360
  • Point and location coordinates are integers in [-10^4, 10^4]
  • Duplicate point coordinates are allowed and count as separate points

Approach: Polar Angles + Sliding Window

Convert each point to its angle relative to the location using atan2. Sort the angles, then duplicate the array with +360° to handle the wraparound. Slide a window of size angle degrees across the sorted list to find the maximum count. Add points that sit exactly on the location separately since they're always visible.

Keep at most n entries in the window, where n is the number of points away from the observer, so the duplicated angle list cannot count a point twice. This also handles a 360-degree view correctly. The small tolerance in the angle comparison absorbs floating-point rounding at inclusive boundaries.

javascript
function visiblePoints(points, angle, location) {
  const [lx, ly] = location;
  let atLocation = 0;
  const angles = [];

  for (const [px, py] of points) {
    if (px === lx && py === ly) {
      atLocation++;
    } else {
      angles.push(Math.atan2(py - ly, px - lx) * (180 / Math.PI));
    }
  }

  angles.sort((a, b) => a - b);
  const n = angles.length;
  const doubled = [...angles, ...angles.map(a => a + 360)];

  let max = 0;
  let left = 0;

  for (let right = 0; right < doubled.length; right++) {
    while (right - left + 1 > n || doubled[right] - doubled[left] > angle + 1e-9) left++;
    max = Math.max(max, right - left + 1);
  }

  return max + atLocation;
}

Time Complexity: O(n log n) for sorting

Space Complexity: O(n) for the angles array

Loading discussion…

Saved in this browser only. Private to you.

JavaScript