Line Reflection - Problem
Line Reflection is a fascinating geometric problem that tests your understanding of symmetry and coordinate geometry.

You're given n points on a 2D plane, and your task is to determine if there exists a vertical line (parallel to the y-axis) that acts as a mirror, such that when you reflect all points across this line, you get back the exact same set of points.

Think of it as placing a mirror vertically on the coordinate plane - if the reflection of your points creates the same pattern, then such a line exists! The key insight is that this line, if it exists, must pass through the center of all points' x-coordinates.

Goal: Return true if such a reflection line exists, false otherwise.
Input: Array of points [[x₁,y₁], [x₂,y₂], ..., [xₙ,yₙ]]
Output: Boolean indicating if reflection line exists

Input & Output

example_1.py — Basic Reflection
$ Input: points = [[1,1],[-1,1]]
Output: true
💡 Note: The reflection line is at x = 0. Point (1,1) reflects to (-1,1) and vice versa. Both reflected points exist in the original set.
example_2.py — No Reflection Possible
$ Input: points = [[1,1],[-1,-1]]
Output: false
💡 Note: If we try the line x = 0, point (1,1) would reflect to (-1,1), but (-1,1) doesn't exist in the original set. The point (-1,-1) would reflect to (1,-1), which also doesn't exist.
example_3.py — Multiple Points Same Line
$ Input: points = [[0,0],[1,0],[3,0]]
Output: false
💡 Note: For symmetry around any line, we need an even number of points or a center point. The potential line would be at x = 1.5, but point (0,0) would reflect to (3,0) and (1,0) would reflect to (2,0). Since (2,0) doesn't exist, no reflection line is possible.

Visualization

Tap to expand
🪞 Reflection Line (x = 400)OriginalReflected✨ Algorithm Steps:1. Find leftmost (250) and rightmost (550) points2. Calculate mirror line: (250 + 550) ÷ 2 = 4003. Verify each point has its reflection
Understanding the Visualization
1
Find Extremes
Identify the leftmost and rightmost points to determine where the mirror should be placed
2
Calculate Center
The mirror line must be at the exact midpoint: (min_x + max_x) / 2
3
Verify Reflections
Check that every point has its mirror image in the original set
Key Takeaway
🎯 Key Insight: The reflection line position is uniquely determined by the extreme points - there's only one possible line to check, making this problem efficiently solvable in O(n) time!

Time & Space Complexity

Time Complexity
⏱️
O(n)

Single pass to build set and find min/max, plus one verification pass

n
2n
Linear Growth
Space Complexity
O(n)

Hash set stores all n points for constant-time lookup

n
2n
Linearithmic Space

Constraints

  • 1 ≤ points.length ≤ 2 × 104
  • points[i].length == 2
  • -108 ≤ points[i][0], points[i][1] ≤ 108
  • There can be repeated points
Asked in
Google 12 Facebook 8 Amazon 6 Microsoft 4
23.4K Views
Medium Frequency
~18 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