Line Reflection - Problem

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: There can be repeated points.

Input & Output

Example 1 — Perfect Symmetry
$ 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 reflections exist in the original set.
Example 2 — No Symmetry
$ Input: points = [[1,1],[-1,-1]]
Output: false
💡 Note: The reflection line would be at x = 0. Point (1,1) reflects to (-1,1), but (-1,1) doesn't exist. Point (-1,-1) reflects to (1,-1), but (1,-1) doesn't exist.
Example 3 — Single Point
$ Input: points = [[0,0]]
Output: true
💡 Note: A single point can always be reflected across a line through itself, so it's symmetric.

Constraints

  • 1 ≤ points.length ≤ 104
  • -108 ≤ points[i][0], points[i][1] ≤ 108

Visualization

Tap to expand
Line Reflection Problem INPUT x y (1,1) (-1,1) Input Points: points = [[1,1],[-1,1]] 2 points on 2D plane ALGORITHM STEPS 1 Find min/max X minX = -1, maxX = 1 2 Calculate center line center = (minX + maxX) / 2 center = (-1 + 1) / 2 = 0 3 Store points in HashSet set = {"1,1", "-1,1"} 4 Check reflection exists For (1,1): reflected = (-1,1) For (-1,1): reflected = (1,1) HashMap Check: "1,1" OK "-1,1" OK All reflections found! FINAL RESULT x=0 (1,1) (-1,1) Output: true Symmetric line exists at x = 0 (parallel to y-axis) Key Insight: The reflection line x = c must be at the center: c = (minX + maxX) / 2. For each point (x, y), its reflection is (2*c - x, y). Use a HashSet to check if all reflected points exist in the original set. Time: O(n), Space: O(n). TutorialsPoint - Line Reflection | Hash Map with Center Line Calculation
Asked in
Google 15 Facebook 12 Amazon 8
28.0K Views
Medium Frequency
~15 min Avg. Time
456 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