# Simple Java 6 line solution

• Simple explanation: find distances between each points. You don't need to find square roots as we don't really care about actual distance as long as each distance is squared. Once we are done with it, we can sort it and check if first 4 entries are same(sides of the square) and last 2 entries(diagonal) are same. The reason diagonal should always be in last 2 as diagonal length is always greater than distance between sides. Last check is if the given square is just a point(i.e all points are(0,0) ), we return false.

``````import java.util.*;
class Solution {
public boolean validSquare(int[] p1, int[] p2, int[] p3, int[] p4) {

int[] lengths = new int[]{findDistance(p1, p2), findDistance(p1, p3), findDistance(p1, p4), findDistance(p2, p3),
findDistance(p2, p4), findDistance(p3, p4)};
Arrays.sort(lengths);
boolean isSquare = lengths[0] == lengths[1] && lengths[1] == lengths[2] && lengths[2] == lengths[3] && lengths[4] == lengths[5];
return isSquare && !(lengths[0] == 0);
}

private int findDistance(int[] p, int[] q) {
return (p[0] - q[0]) * (p[0] - q[0]) + (p[1] - q[1]) * (p[1] - q[1]);
}
}
``````

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.