Maximum Number of Visible Points
Maximum Number of Visible Points is a hard arrays & hashing problem solved with the sliding window · variable pattern.
The best approach, optimal (sort angles + sliding window over a doubled circle), runs in O(n log n) time and O(n) space.
Below are 2 approaches in Java, Python, C++ and JavaScript, from brute force (try every point as the window start) up.
Problem
You stand at location = [x, y] on a plane and are given a list of integer points. You cannot move, but you can turn to face any direction. Your field of view is a wedge angle degrees wide: if you face direction d (measured counterclockwise from the positive x-axis), you see every point whose direction from you lies in [d - angle / 2, d + angle / 2], edges included.
Points at your exact location are always visible, whatever direction you face. Several points may share a position, and points never block one another.
Return the maximum number of points you can see at once.
Examples
Example 1
- Input
points = [[4, 2], [3, 3], [2, 4], [0, 2], [2, 0]], angle = 90, location = [2, 2]- Output
3- Why
- Seen from [2, 2] the points lie at 0°, 45°, 90°, 180° and 270°. Facing 45° covers 0° to 90°, which holds [4, 2], [3, 3] and [2, 4]; no 90° wedge holds more.
Example 2
- Input
points = [[2, 2], [5, 5], [0, 4]], angle = 0, location = [2, 2]- Output
2- Why
- [2, 2] sits on your location and is always visible. A zero-width view sees one ray: aim it at [5, 5] (or at [0, 4]) for 1 + 1 = 2.
Example 3
- Input
points = [[0, 3], [0, 1], [4, 2]], angle = 60, location = [2, 2]- Output
2- Why
- [0, 3] and [0, 1] are at about 153° and 207°; facing straight left (180°) sees both, while [4, 2] is behind you.
Constraints
1 <= points.length <= 10^5points[i].length == 2,location.length == 20 <= angle < 3600 <= x, y <= 100for every point and for the location
Solve it here
Write your solution in Java, Python, C++ or JavaScript and run it against the examples, right in the page. Submit runs the hidden tests too and, with a free account, records the solve in your revision log.
Your own testcases: Run checks these, and the expected output comes from our reference solution.
My submissions
Nothing yet. Every Run and Submit you make while signed in shows up here.
| When | Result | Tests | Runtime | Your time | Type |
|---|
Run checks the testcases above (3 examples, or your own). Submit also runs 14 hidden tests and saves the solve to your log. Signed in, every Run and Submit is saved under My submissions; your draft stays in this browser.
Solutions
Try it yourself first. Then compare: each approach lists its idea, the steps, its time and space, and the code in Java, Python, C++ and JavaScript.
| Approach | Time | Space |
|---|---|---|
| Brute force (try every point as the window start) | O(n²) | O(n) |
| Optimal (sort angles + sliding window over a doubled circle) | O(n log n) | O(n) |
1Brute force (try every point as the window start)
O(n²)Every pair of directions is compared.O(n)The list of angles.An optimal view can always be rotated until its left edge touches some point. So try each point's direction as the start and count how many directions fall within angle degrees counterclockwise of it, wrapping around 360.
- same = points equal to location; compute angles (degrees) of the others.
- For each angle a: count angles b with (b - a) mod 360 <= angle (with a tiny tolerance).
- Return same + the best count.
class Solution {
public int visiblePoints(List<List<Integer>> points, int angle, List<Integer> location) {
int ox = location.get(0), oy = location.get(1), same = 0;
List<Double> angles = new ArrayList<>();
for (List<Integer> p : points) {
int dx = p.get(0) - ox, dy = p.get(1) - oy;
if (dx == 0 && dy == 0) same++;
else angles.add(Math.atan2(dy, dx) * 180 / Math.PI);
}
int best = 0;
for (double a : angles) {
int count = 0;
for (double b : angles) {
double diff = b - a;
if (diff < -1e-9) diff += 360;
if (diff <= angle + 1e-9) count++;
}
best = Math.max(best, count);
}
return same + best;
}
}class Solution:
def visiblePoints(self, points: List[List[int]], angle: int, location: List[int]) -> int:
ox, oy = location
same = 0
angles = []
for x, y in points:
dx, dy = x - ox, y - oy
if dx == 0 and dy == 0:
same += 1
else:
angles.append(math.atan2(dy, dx) * 180 / math.pi)
best = 0
for a in angles:
count = 0
for b in angles:
diff = b - a
if diff < -1e-9:
diff += 360
if diff <= angle + 1e-9:
count += 1
best = max(best, count)
return same + bestclass Solution {
public:
int visiblePoints(vector<vector<int>>& points, int angle, vector<int>& location) {
int ox = location[0], oy = location[1], same = 0;
vector<double> angles;
for (auto& p : points) {
int dx = p[0] - ox, dy = p[1] - oy;
if (dx == 0 && dy == 0) same++;
else angles.push_back(atan2((double)dy, (double)dx) * 180 / M_PI);
}
int best = 0;
for (double a : angles) {
int count = 0;
for (double b : angles) {
double diff = b - a;
if (diff < -1e-9) diff += 360;
if (diff <= angle + 1e-9) count++;
}
best = max(best, count);
}
return same + best;
}
};function visiblePoints(points, angle, location) {
const [ox, oy] = location;
let same = 0;
const angles = [];
for (const [x, y] of points) {
const dx = x - ox, dy = y - oy;
if (dx === 0 && dy === 0) same++;
else angles.push(Math.atan2(dy, dx) * 180 / Math.PI);
}
let best = 0;
for (const a of angles) {
let count = 0;
for (const b of angles) {
let diff = b - a;
if (diff < -1e-9) diff += 360;
if (diff <= angle + 1e-9) count++;
}
best = Math.max(best, count);
}
return same + best;
}2Optimal (sort angles + sliding window over a doubled circle)
O(n log n)Sorting dominates; the window pass is linear because each pointer only moves forward.O(n)The doubled angle list.Sort the directions, then append each one again plus 360 so a window that wraps past 0° becomes an ordinary contiguous window. Slide a two-pointer window whose width (last - first) stays within angle; the largest window, plus the points on your location, is the answer.
- same = points on the location; angles = sorted directions of the rest (degrees).
- Append angles[i] + 360 for every i.
- For right over the doubled list: while ext[right] - ext[left] > angle + 1e-9, left++; best = max(best, right - left + 1).
- Return same + min(best, number of angles).
class Solution {
public int visiblePoints(List<List<Integer>> points, int angle, List<Integer> location) {
int ox = location.get(0), oy = location.get(1), same = 0;
List<Double> angles = new ArrayList<>();
for (List<Integer> p : points) {
int dx = p.get(0) - ox, dy = p.get(1) - oy;
if (dx == 0 && dy == 0) same++;
else angles.add(Math.atan2(dy, dx) * 180 / Math.PI);
}
Collections.sort(angles);
int m = angles.size();
double[] ext = new double[2 * m];
for (int i = 0; i < m; i++) {
ext[i] = angles.get(i);
ext[i + m] = angles.get(i) + 360;
}
int best = 0, left = 0;
for (int right = 0; right < 2 * m; right++) {
while (ext[right] - ext[left] > angle + 1e-9) left++;
best = Math.max(best, right - left + 1);
}
return same + Math.min(best, m);
}
}class Solution:
def visiblePoints(self, points: List[List[int]], angle: int, location: List[int]) -> int:
ox, oy = location
same = 0
angles = []
for x, y in points:
dx, dy = x - ox, y - oy
if dx == 0 and dy == 0:
same += 1
else:
angles.append(math.atan2(dy, dx) * 180 / math.pi)
angles.sort()
m = len(angles)
ext = angles + [a + 360 for a in angles]
best = left = 0
for right in range(2 * m):
while ext[right] - ext[left] > angle + 1e-9:
left += 1
best = max(best, right - left + 1)
return same + min(best, m)class Solution {
public:
int visiblePoints(vector<vector<int>>& points, int angle, vector<int>& location) {
int ox = location[0], oy = location[1], same = 0;
vector<double> angles;
for (auto& p : points) {
int dx = p[0] - ox, dy = p[1] - oy;
if (dx == 0 && dy == 0) same++;
else angles.push_back(atan2((double)dy, (double)dx) * 180 / M_PI);
}
sort(angles.begin(), angles.end());
int m = angles.size();
vector<double> ext(2 * m);
for (int i = 0; i < m; i++) {
ext[i] = angles[i];
ext[i + m] = angles[i] + 360;
}
int best = 0, left = 0;
for (int right = 0; right < 2 * m; right++) {
while (ext[right] - ext[left] > angle + 1e-9) left++;
best = max(best, right - left + 1);
}
return same + min(best, m);
}
};function visiblePoints(points, angle, location) {
const [ox, oy] = location;
let same = 0;
const angles = [];
for (const [x, y] of points) {
const dx = x - ox, dy = y - oy;
if (dx === 0 && dy === 0) same++;
else angles.push(Math.atan2(dy, dx) * 180 / Math.PI);
}
angles.sort((a, b) => a - b);
const m = angles.length;
const ext = angles.concat(angles.map((a) => a + 360));
let best = 0, left = 0;
for (let right = 0; right < 2 * m; right++) {
while (ext[right] - ext[left] > angle + 1e-9) left++;
best = Math.max(best, right - left + 1);
}
return same + Math.min(best, m);
}Edge cases to test
- Points exactly on your location: always counted, and they have no direction
- angle = 0: only points on one exact ray count
- A best window that wraps across the 0° / 360° direction
- Several points on the same ray, and points exactly on the edge of the window
Hints
Hint 1
Only the direction of each point matters. Turn every point into an angle with atan2 relative to your location.
Hint 2
After sorting the angles, a field of view is a window [a, a + angle]. Which data structure finds the most values inside a fixed-width window?
Hint 3
To handle windows that wrap past 360°, append every angle again plus 360 and slide over the doubled list.
FAQ
What is the best time complexity for Maximum Number of Visible Points?
Optimal (sort angles + sliding window over a doubled circle) runs in O(n log n) time and O(n) extra space. Sorting dominates; the window pass is linear because each pointer only moves forward.
Which pattern does Maximum Number of Visible Points use?
It is a arrays & hashing problem that uses the sliding window · variable pattern. Other problems with the same pattern: Minimum Size Subarray Sum, Count of Subarrays with Product Less Than K, Longest Repeating Character Replacement.
Is there a brute force solution for Maximum Number of Visible Points?
Yes. Brute force (try every point as the window start) takes O(n²) time and O(n) space. An optimal view can always be rotated until its left edge touches some point.
Which edge cases should I test for Maximum Number of Visible Points?
Points exactly on your location: always counted, and they have no direction; angle = 0: only points on one exact ray count; A best window that wraps across the 0° / 360° direction; Several points on the same ray, and points exactly on the edge of the window.