Given n points on a 2D plane, find if there is such a line parallel to y-axis that reflect the given points.
Example 1:
Givenpoints=[[1,1],[-1,1]], returntrue.
Example 2:
Givenpoints=[[1,1],[-1,-1]], returnfalse.
Follow up:
Could you do better than O(n^2)?
The Idea: First iterate through the list of points to find the most left and the most right x coordinates. The respected axis for between these two points must be 1/2 the distance between these points from left coordinate. It also must follow that the remaining points are a reflection of this axis. In order compare against this axis, I allocated an unordered_set of points, so that I can quickly lookup any point by its unique string permutation (x,y). So then, in order to verify the reflection, I iterate through each point and check to see whether its reflected analog exists through in the unordered_set through a simple lookup. If all the reflected analogs exists, return true.
There are a few edge cases:
1. Empty set is a valid reflection.
2. Checking for points that exist along the axis of reflection.