You are given an array points, an integer angle, and your location, where location = [posx, posy] and 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.
Example 1:
Input: points = [[2,1],[2,2],[3,3]], angle = 90, location = [1,1] Output: 3 Explanation: The shaded region represents your field of view. All points can be made visible in your field of view, including [3,3] even though [2,2] is in front and in the same line of sight.
Example 2:
Input: points = [[2,1],[2,2],[3,4],[1,1]], angle = 90, location = [1,1] Output: 4 Explanation: All points can be made visible in your field of view, including the one at your location.
Example 3:
Input: points = [[1,0],[2,1]], angle = 13, location = [1,1] Output: 1 Explanation: You can only see one of the two points, as shown above.
Constraints:
1 <= points.length <= 105points[i].length == 2location.length == 20 <= angle < 3600 <= posx, posy, xi, yi <= 100The key idea for #1610 Maximum Number of Visible Points is to transform the geometric visibility problem into an angle-based sliding window problem. For every point relative to the observer’s location, compute the angle formed with the horizontal axis using atan2. Points that share the same location as the observer are always visible and should be counted separately.
Once the angles are computed, sort them to analyze visibility ranges efficiently. Because the viewing angle can wrap around (for example near 0° and 360°), duplicate the angle list by adding 2π to each value. This helps simulate circular rotation without special edge handling.
Then apply a sliding window over the sorted angles to find the maximum number of points whose angular difference fits within the given viewing angle. The window expands while the difference stays within the limit and shrinks otherwise. Finally, add the count of overlapping points at the observer’s location. The dominant cost comes from sorting the angles.
| Approach | Time Complexity | Space Complexity |
|---|---|---|
| Angle Conversion + Sorting + Sliding Window | O(n log n) | O(n) |
NeetCodeIO
Use these hints if you're stuck. Try solving on your own first.
Sort the points by polar angle with the original position. Now only a consecutive collection of points would be visible from any coordinate.
We can use two pointers to keep track of visible points for each start point
For handling the cyclic condition, it’d be helpful to append the point list to itself after sorting.
Watch expert explanations and walkthroughs
Practice problems asked by these companies to ace your technical interviews.
Explore More ProblemsJot down your thoughts, approach, and key learnings
Yes, variations of this problem can appear in top tech interviews because it combines geometry, sorting, and sliding window techniques. It tests a candidate’s ability to transform geometric problems into manageable algorithmic patterns.
Angles are duplicated by adding 2π to simulate circular rotation. This avoids edge cases where visible points span across the 0° boundary and allows a simple sliding window to work on a linear array.
The optimal approach converts each point into an angle relative to the observer using trigonometry. After sorting these angles, a sliding window is used to find the largest group within the allowed viewing angle. This method efficiently handles circular angle ranges.
An array or list is sufficient to store computed angles. After sorting the angles, two pointers are used for the sliding window technique to maintain the visible range efficiently.