Similar Problems

Similar Problems not available

Line Reflection - Leetcode Solution


LeetCode:  Line Reflection Leetcode Solution

Difficulty: Medium

Topics: math hash-table array  

The Line Reflection problem on LeetCode is a medium difficulty problem that requires you to check if a set of points can be reflected across a straight line. Here is a detailed solution for the problem:

Problem Statement

Given n points on a 2D plane, find if there exists a line such that the points can be reflected across the line.

You may assume that:

  • There are at most 50 points.
  • Each point given is an integer given in the range [-10^4, 10^4].
  • A point is reflected across a line x = c as (c-x, y).

Solution Approach

The first step is to check whether there are at least two points provided. If there are less than two points, there won't be any line to reflect across. If this is the case, we can simply return true.

Once we have established that there are at least two points available, we need to find the line of symmetry. If a line of symmetry exists, the midpoint of two points which are reflections of each other will lie on the line of symmetry.

We can find the midpoint of two points by taking the average of their x and y coordinates separately. Let's call the midpoint coordinates as (mx, my).

Now, if a line of symmetry exists, for any two points (x1, y1) and (x2, y2) such that (x2 - x1, y2 - y1), their reflection coordinates will be (2 * mx - x1, 2 * my - y1), and (2 * mx - x2, 2 * my - y2), respectively.

We can sort all the points lexicographically and check whether all the pairs of points have their reflections lying on the line of symmetry.


The following is the pseudo-code to solve this problem:

  1. Check whether the length of the points list is less than 2, return true if yes.
  2. Find the midpoint of any two points in the list.
  3. For all pairs of points (p1, p2) with p2 > p1:
  • Find their reflection coordinates (rx1, ry1) and (rx2, ry2).
  • If any of the reflection coordinates do not fall on the line of symmetry, return false.
  1. Return true if all points have been reflected across the line of symmetry.

Time and Space Complexity

The time complexity of this algorithm is O(n log n) since we have to sort the points. The space complexity is O(n) because we are storing the points in a list.


The line reflection problem on Leetcode requires you to find whether a set of points can be reflected across a straight line. The approach involves identifying a line of symmetry and reflecting each pair of points around that line. This problem can be solved efficiently with O(n log n) time and O(n) space complexity.

Line Reflection Solution Code