Maximum Number of Visible Points - Problem

You are given an array points, an integer angle, and your location location = [posx, posy] where points[i] = [xi, yi] both denote integral coordinates on the X-Y plane.

Initially, you are facing directly east from your position. You cannot move from your position, but you can rotate. In other words, posx and posy cannot be changed. Your field of view in degrees is represented by angle, determining how wide you can see from any given view direction.

Let d be the amount in degrees that you rotate counterclockwise. Then, your field of view is the inclusive range of angles [d - angle/2, d + angle/2].

You can see some set of points if, for each point, the angle formed by the point, your position, and the immediate east direction from your position is in your field of view.

There can be multiple points at one coordinate. There may be points at your location, and you can always see these points regardless of your rotation. Points do not obstruct your vision to other points.

Return the maximum number of points you can see.

Input & Output

Example 1 — Basic Field of View
$ Input: points = [[2,1],[2,2],[3,3]], angle = 90, location = [1,1]
Output: 3
💡 Note: With a 90° field of view, we can rotate to capture all 3 points. The optimal rotation direction allows us to see points at angles that fit within the 90° range.
Example 2 — Narrow View
$ Input: points = [[2,1],[2,2],[3,4],[1,1]], angle = 30, location = [1,1]
Output: 2
💡 Note: Point [1,1] is at our location (always visible). With 30° field of view, we can see at most 1 additional point, for total of 2.
Example 3 — Wide View
$ Input: points = [[1,0],[2,1]], angle = 180, location = [1,1]
Output: 2
💡 Note: With 180° field of view (half circle), we can see both points by rotating to an appropriate direction.

Constraints

  • 1 ≤ points.length ≤ 103
  • points[i].length == 2
  • location.length == 2
  • -109 ≤ xi, yi ≤ 109
  • 0 ≤ angle < 360

Visualization

Tap to expand
Maximum Number of Visible Points INPUT X Y You(1,1) (2,1) (2,2) (3,3) East points = [[2,1],[2,2],[3,3]] angle = 90 location = [1,1] Field of view: 90 degrees ALGORITHM STEPS 1 Calculate Angles Use atan2 for each point (2,1): atan2(0,1) = 0 deg (2,2): atan2(1,1) = 45 deg (3,3): atan2(2,2) = 45 deg 2 Sort Angles Sort + duplicate for wrap [0, 45, 45] sorted 3 Sliding Window Find max points in range window: [d-45, d+45] All 3 points fit in 90 deg 4 Add Same Location Points at location always seen max_visible = 3 + 0 = 3 FINAL RESULT You 90 deg view OUTPUT 3 All 3 points visible in 90 degree view [OK] Verified Key Insight: Convert problem to circular array sliding window. Calculate angle from location to each point using atan2. Sort angles, duplicate array with +360 for wrap-around. Use two pointers to find max points within angle range. Time Complexity: O(n log n) for sorting. Space: O(n) for storing angles. TutorialsPoint - Maximum Number of Visible Points | Optimal Solution
Asked in
Google 12 Facebook 8 Amazon 6
28.4K Views
Medium Frequency
~25 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen