This website contains ALL LeetCode **Premium** problems for
**FREE!!**.

All leaked interview problems are collected from Internet.

All leaked interview problems are collected from Internet.

Given the coordinates of four points in 2D space, return whether the four points could construct a square.

The coordinate (x,y) of a point is represented by an integer array with two integers.

**Example:**

Input:p1 = [0,0], p2 = [1,1], p3 = [1,0], p4 = [0,1]Output:True

Note:

- All the input integers are in the range [-10000, 10000].
- A valid square has four equal sides with positive length and four equal angles (90-degree angles).
- Input points have no order.

b'

\n## Solution

\n

\n#### Approach #1 Brute Force [Accepted]

\n

\n#### Approach #2 Using Sorting [Accepted]

\n

\n#### Approach #3 Checking every case [Accepted]

\n

\n

'
\n\n

\n\n

The idea behind determining whether 4 given set of points constitute a valid square or not is really simple. Firstly, we need to determine if the sides of the qaudrilateral formed by these 4 points are equal. But checking only this won\'t suffice. Since, this condition will be satisfied even in the case of a rhombus, where all the four sides are equal but the adjacent sides aren\'t perpendicular to each other. Thus, we also need to check if the lengths of the diagonals formed between the corners of the quadrilateral are equal. If both the conditions are satisfied, then only the given set of points can be deemed appropriate for constituting a square.

\nNow, the problem arises in determining which pairs of points act as the adjacent points on the square boundary. So, the simplest method is to consider every possible case. For the given 4 points, , there are a total of 4! ways in which these points can be arranged to be considered as the square\'s boundaries. We can generate every possible permutation and check if any permutation leads to the valid square arrangement of points.

\n\n**Complexity Analysis**

- \n
- \n
Time complexity : . Constant number of permutations() are generated.

\n \n - \n
Space complexity : . Constant space is required.

\n \n

\n

Instead of considering all the permutations of arrangements possible, we can make use of maths to simplify this problem a bit. If we sort the given set of points based on their x-coordinate values, and in the case of a tie, based on their y-coordinate value, we can obtain an arrangement, which directly reflects the arrangement of points on a valid square boundary possible.

\nConsider the only possible cases as shown in the figure below:

\n\nIn each case, after sorting, we obtain the following conclusion regarding the connections of the points:

\n- \n
- \n
\n, , and form the four sides of any valid square.

\n \n - \n
\n and form the diagonals of the square.

\n \n

Thus, once the sorting of the points is done, based on the above knowledge, we can directly compare , , and for equality of lengths(corresponding to the sides); and and for equality of lengths(corresponding to the diagonals).

\n\n**Complexity Analysis**

- \n
- \n
Time complexity : . Sorting 4 points takes constant time.

\n \n - \n
Space complexity : . Constant space is required.

\n \n

\n

**Algorithm**

If we consider all the permutations descripting the arrangement of points as in the brute force approach, we can come up with the following set of 24 arrangements:

\n\nIn this figure, the rows with the same shaded color indicate that the corresponding arrangements lead to the same set of edges and diagonals. Thus, we can see that only three unique cases exist. Thus, instead of generating all the 24 permutations, we check for the equality of edges and diagonals for only the three distinct cases.

\n\n**Complexity Analysis**

- \n
- \n
Time complexity : . A fixed number of comparisons are done.

\n \n - \n
Space complexity : . No extra space required.

\n \n

\n

Analysis written by: @vinod23

\n