# 4 Liner Java

• ``````public boolean validSquare(int[] p1, int[] p2, int[] p3, int[] p4) {
HashSet<Integer> hs = new HashSet<>(Arrays.asList(dis(p1, p2), dis(p1, p3), dis(p1, p4), dis(p2, p3), dis(p2, p4), dis(p3, p4)));
return !hs.contains(0) && hs.size()==2; //One each for side & diagonal
}
int dis(int[] a, int[] b){
return (a[0]-b[0])*(a[0]-b[0]) + (a[1]-b[1])*(a[1]-b[1]);
}
``````

• Alternatively:

``````public boolean validSquare(int[] p1, int[] p2, int[] p3, int[] p4) {

int[] arr = {dis(p1, p2), dis(p1, p3), dis(p1, p4), dis(p2, p3), dis(p2, p4), dis(p3, p4)};
Arrays.sort(arr);

return arr[0] > 0 && arr[0] == arr[1] && arr[1] == arr[2] && arr[2] == arr[3] && arr[4] == arr[5];
}

int dis(int[] a, int[] b) {
return (a[0]-b[0])*(a[0]-b[0]) + (a[1]-b[1])*(a[1]-b[1]);
}``````

• @aayushgarg
it is wrong to judge by size() == 2, as 4 points can be the Equilateral triangle and one point in the middle. It also has 2 same values, the size is 2. But it is triangle not square

• @yiminghuang It is not possible to construct an equilateral triangle with vertices on lattice points (a point at the intersection of two or more grid lines). Since we have been given integer array, equilateral triangle case will not exist.
Proof: https://math.stackexchange.com/questions/105330/equilateral-triangle-whose-vertices-are-lattice-points
See using: http://triancal.esy.es/?lang=en&a=2&b=2&c=2&A=60°&B=60°

• Your code couldn't pass this test case:
A Parallelogram, 4 sides are equal and also equal to the short diagonal, which could also return true in your code.

• @Shinia Unfortunately, if the short diagonal equals the 4 sides, 3 points would also be on an equilateral triangle, which is impossible since the input points are integer array.

• The idea is brilliant and the math behind supports the argument that for integer inputs it is IMPOSSIBLE to construct a diamond with an inner angle 60 degrees.
The code itself it robust.

I just wrote a stupid one but it is easy to understand.

``````public class Solution {
public boolean validSquare(int[] p1, int[] p2, int[] p3, int[] p4) {
HashMap<Integer, Integer> map = new HashMap<>();
int a12 = getDistanceSquare(p1, p2);
int a13 = getDistanceSquare(p1, p3);
int a14 = getDistanceSquare(p1, p4);
int a23 = getDistanceSquare(p2, p3);
int a24 = getDistanceSquare(p2, p4);
int a34 = getDistanceSquare(p3, p4);
if (a12 == 0 || a13 == 0 || a14 == 0 || a23 == 0 || a24 == 0 || a34 == 0)   return false;
map.put(a12, map.getOrDefault(a12, 0) + 1);
map.put(a13, map.getOrDefault(a13, 0) + 1);
map.put(a14, map.getOrDefault(a14, 0) + 1);
map.put(a23, map.getOrDefault(a23, 0) + 1);
map.put(a24, map.getOrDefault(a24, 0) + 1);
map.put(a34, map.getOrDefault(a34, 0) + 1);
return map.size() == 2 && (map.get(a12) == 2 || map.get(a12) == 4);

}

private int getDistanceSquare(int[] a, int[] b) {
return (a[0] - b[0]) * (a[0] - b[0]) + (a[1] - b[1]) * (a[1] - b[1]);
}
}
``````

• This post is deleted!

• Similar solution but using ArrayList.

``````class Solution {
public boolean validSquare(int[] p1, int[] p2, int[] p3, int[] p4) {
ArrayList<Integer> list = new ArrayList();
setLength(p1,p2, list);
setLength(p1,p3, list);
setLength(p1,p4, list);
setLength(p2,p3, list);
setLength(p2,p4, list);
setLength(p3,p4, list);

if(list.size()!=2)
return false;

if(list.get(0) == 0 || list.get(1) == 0)
return false;

if(list.get(0) / list.get(1) == 2 ||
list.get(0) / list.get(1) == 1/2 )
return true;
return false;
}

public void setLength(int[] p1, int[]p2, ArrayList<Integer> list){
int l1 = p1[0] - p2[0];
int l2 = p1[1] - p2[1];
int len = l1*l1 + l2*l2;

if(!list.contains(len))