Problem Description
Given n points on a 2D plane, determine if there exists a vertical line (parallel to the y-axis) such that when all points are reflected over this line, the set of reflected points equals the original set of points.
Key Insights
- The candidate reflection line must be vertical (x = c) and is determined by the average of the minimum and maximum x-coordinates of the points.
- For each point, its reflected counterpart would have an x-coordinate of 2*c - x while the y-coordinate remains unchanged.
- A hash set (or similar data structure) can be used to store the points to allow O(1) lookup of the reflected point.
- Checking every point against its reflection helps decide if the reflection symmetry exists.
Space and Time Complexity
Time Complexity: O(n) - We iterate once to determine min and max, and then once over all points. Space Complexity: O(n) - We store all points in a set for constant time lookup.
Solution
The solution involves first determining the candidate line of reflection by finding the minimum and maximum x-coordinates among all the points and computing their average. This candidate line is given by x = (min_x + max_x) / 2. For each point, we compute its reflected coordinate as (2*candidate - x, y) and verify if this reflected point exists in the set of original points. The use of a hash table (or set) ensures that each lookup is done in constant time, resulting in an overall linear time solution with respect to the number of points.