Given n points on a 2D plane, find if there is such a line parallel to the y-axis that reflects the given points symmetrically.
In other words, answer whether or not if there exists a line that after reflecting all points over the given line, the original points' set is the same as the reflected ones.
Note that there can be repeated points.
Example 1:
Input: points = [[1,1],[-1,1]] Output: true Explanation: We can choose the line x = 0.
Example 2:
Input: points = [[1,1],[-1,-1]] Output: false Explanation: We can't choose a line.
Constraints:
n == points.length1 <= n <= 104-108 <= points[i][j] <= 108
Follow up: Could you do better than O(n2)?
Problem Overview: You are given a list of 2D points on a plane. The task is to determine whether there exists a vertical line such that every point has its mirror reflection across that line within the same set of points.
Approach 1: Pairwise Reflection Check (Brute Force) (Time: O(n2), Space: O(1))
A direct way is to test whether a possible reflection line works for all points. One candidate line can be derived from the average of two points' x-coordinates. For each candidate line x = c, iterate through all points and verify that their mirrored coordinate (2c - x, y) exists somewhere in the list. Because checking existence requires scanning the array, each lookup costs O(n). This leads to O(n2) time in the worst case. The approach uses constant extra memory but becomes slow as the number of points grows.
Approach 2: Hash Set with Min/Max Reflection Line (Optimal) (Time: O(n), Space: O(n))
The key observation: if a vertical reflection line exists, it must lie exactly halfway between the smallest and largest x-coordinates in the set. Compute minX and maxX by iterating once through the points. The potential reflection line becomes x = (minX + maxX) / 2. Store all points in a hash set using a string or tuple representation like (x, y).
Now iterate through each point again. For a point (x, y), its mirrored partner across the line must be (minX + maxX - x, y). Perform a constant-time hash lookup to confirm the mirrored point exists. If any reflected point is missing, symmetry fails immediately. If all points pass the check, the set is symmetric across the computed line.
This approach leverages a hash table for O(1) lookups and uses a simple math observation about reflection. The input is processed with linear scans of the array, giving O(n) time complexity and O(n) additional space for the set.
Recommended for interviews: The hash-set reflection approach is the expected solution. It shows that you recognize the geometric property of the reflection line and can combine it with constant-time membership checks. Mentioning the brute-force idea first demonstrates understanding of the symmetry requirement, but implementing the O(n) hash-based method shows stronger algorithmic reasoning.
Java
C++
Go
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Pairwise Reflection Check (Brute Force) | O(n^2) | O(1) | Useful for understanding the reflection concept or when constraints are very small |
| Hash Set with Min/Max Reflection Line | O(n) | O(n) | General optimal solution with fast lookups for mirrored points |
How to EASILY solve LeetCode problems • NeetCode • 427,689 views views
Watch 9 more video solutions →Practice Line Reflection with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor