using
System;
using
System.Collections.Generic;
public
class
MyEqualityComparer : IEqualityComparer<
int
[]>
{
public
bool
Equals(
int
[] x,
int
[] y)
{
if
(x.Length != y.Length)
{
return
false
;
}
for
(
int
i = 0; i < x.Length; i++)
{
if
(x[i] != y[i])
{
return
false
;
}
}
return
true
;
}
public
int
GetHashCode(
int
[] obj)
{
int
result = 17;
for
(
int
i = 0; i < obj.Length; i++)
{
unchecked
{
result = result * 23 + obj[i];
}
}
return
result;
}
}
class
GFG
{
static
void
findLargestSquare(
int
[,] points,
int
n)
{
Dictionary<
int
[],
int
> m =
new
Dictionary<
int
[],
int
>(
new
MyEqualityComparer());
for
(
int
i = 0; i < n; i++) {
int
[] l1 = {points[i, 0], points[i, 1]};
if
(!m.ContainsKey(l1))
m[l1] = 0;
m[l1]++;
}
int
side = -1, x = -1, y = -1;
for
(
int
i = 0; i < n; i++) {
int
[] l1= {points[i, 0], points[i, 1]};
m[l1]--;
for
(
int
j = 0; j < n; j++) {
int
[] l2 = {points[j,0], points[j,1]};
m[l2]--;
int
[] l3 = {points[i,0], points[j,1]};
int
[] l4 = {points[j,0], points[i,1]};
if
(i != j
&& (points[i,0]-points[j,0]) == (points[i,1]-points[j,1])){
if
(m.ContainsKey(l3)
&& m.ContainsKey(l4)) {
if
(side < Math.Abs(points[i,0] - points[j,0])
|| (side == Math.Abs(points[i,0] - points[j,0])
&& ((points[i,0] * points[i,0]
+ points[i,1] * points[i,1])
< (x * x + y * y)))) {
x = points[i,0];
y = points[i,1];
side = Math.Abs(points[i,0] - points[j,0]);
}
}
}
m[l2]++;
}
m[l1]++;
}
if
(side != -1)
Console.WriteLine(
"Side of the square is : "
+ side +
", \npoints of the square are "
+ x +
", "
+ y
+
" "
+ (x + side) +
", "
+ y
+
" "
+ (x) +
", "
+ (y + side)
+
" "
+ (x + side) +
", "
+ (y + side));
else
Console.WriteLine(
"No such square"
);
}
public
static
void
Main(
string
[] args)
{
int
n = 6;
int
[,] points = { { 1, 1 }, { 4, 4 }, { 3, 4 }, { 4, 3 }, { 1, 4 }, { 4, 1 } };
findLargestSquare(points, n);
}
}