We are given exactly four points and need to determine whether they form a valid square. Three details make this less trivial than it looks. The points can come in any order, so we cannot assume which points are adjacent and which are opposite corners. The square can be rotated at any angle, not only axis-aligned. And we have to reject the degenerate case where points coincide, since four identical points would otherwise pass a naive equal-sides test with sides of length zero.
The central question is how to check the square property without knowing which pairs of points form sides versus diagonals. Among any four points there are exactly 6 pairwise distances. A valid square produces exactly two distinct distance values: 4 of the distances are equal (the sides) and 2 are equal (the diagonals), with the diagonal equal to sqrt(2) times the side. Comparing squared distances instead of actual distances keeps everything in integers and avoids floating-point rounding.
-10^4 <= x_i, y_i <= 10^4 means a coordinate difference can reach 2 10^4, so a squared distance can reach (2 10^4)^2 + (2 10^4)^2 = 8 10^8. That fits in a signed 32-bit integer (max about 2.1 * 10^9), so plain int would not overflow here. The solutions below still use 64-bit integers for the distance, which removes any doubt about the intermediate products and costs nothing at this input size.If we knew the points in corner order, the check would be straightforward: a quadrilateral ABCD is a square when all four sides (AB, BC, CD, DA) are equal, both diagonals (AC, BD) are equal, and the side length is positive. The positive-length condition excludes the degenerate case of four identical points, which would otherwise have four equal "sides" of length zero.
We do not know the corner order, so we try every order. Fixing p1 as the first corner and permuting the remaining three points gives 3! = 6 candidate orderings. If any ordering satisfies the square check, the points form a square.
Why does equal sides plus equal diagonals force a square, rather than allowing some other shape? A quadrilateral with four equal sides is a rhombus. A rhombus is a square exactly when its diagonals are also equal, because equal diagonals in a rhombus force the interior angles to 90 degrees. So the two conditions together are sufficient.
Permuting the points works, but the ordering is an artifact of how we set up the check, not part of the problem. The next approach drops ordering entirely and characterizes a square by the multiset of its 6 pairwise distances.
Among 4 points there are C(4,2) = 6 pairwise distances. A valid square has 4 equal sides and 2 equal diagonals, with the diagonal strictly longer than the side (in squared terms, diagonal^2 = 2 * side^2), and a positive side length. None of this depends on which points are adjacent, so we can compute all 6 squared distances, sort them, and test the pattern directly. After sorting, the first 4 values are the sides and the last 2 are the diagonals.
The side and diagonal counts alone do not guarantee a square, which is why the relation between them matters. Suppose the 6 squared distances are 4 copies of a value s2 (the squared side) and 2 copies of d2 (the squared diagonal). Four equal sides make the shape a rhombus. The parallelogram law relates the diagonals to the sides: the sum of the squared diagonals equals twice the sum of two squared adjacent sides, which for a rhombus with equal diagonals gives 2 * d2 = 4 * s2, so d2 = 2 * s2. This equality holds exactly when the diagonals are equal, and equal diagonals in a rhombus force the angles to 90 degrees, making it a square. The check d2 == 2 * s2 therefore rejects rhombi with the right counts but wrong angles, and s2 > 0 rejects coincident points.
The next approach replaces the sort with a frequency map, which counts how many times each distance appears and states the square condition in those terms directly.
Instead of sorting, we compute all 6 pairwise squared distances and group them by value in a frequency map. A valid square has exactly 2 distinct distance values: the smaller appears 4 times (the sides) and the larger appears 2 times (the diagonals). The smaller must be positive, and the larger must equal twice the smaller.
The counts read directly off the map, so the code states the square condition (4 sides, 2 diagonals, right angle) without first sorting or indexing into a fixed layout.
side be the smaller value and diag be the larger value.side appears 4 times, diag appears 2 times, side > 0, and diag == 2 * side.