Given ‘n’ pair of points, the task is to find four points such that they form a square whose sides are parallel to x and y axes or print “No such square” otherwise.

If more than one square is possible then choose the one with the maximum area.

**Examples:**

Input :n = 6, points = (1, 1), (4, 4), (3, 4), (4, 3), (1, 4), (4, 1)

Output :

side of the square is : 3, points of the square are 1, 1 4, 1 1, 4 4, 4

Explanation:The points 1, 1 4, 1 1, 4 4, 4 form a square of side 3

Input :n= 6, points= (1, 1), (4, 5), (3, 4), (4, 3), (7, 4), (3, 1)

Output :No such square

**Simple approach: ** Choose all possible pairs of points with four nested loops and then see if the points form a square which is parallel to principal axes. If yes then check if it is the largest square so far in terms of the area and store the result, and then display the result at the end of the program.

**Time Complexity: **O(N^4)

**Efficient Approach:** Create a nested loop for top right and bottom left corner of the square and form a square with those two points, then check if the other two points which were assumed actually exist. To check if a point exists or not, create a map and store the points in the map to reduce the time to check whether the points exist. Also, keep in check the largest square by area so far and print it in the end.

Below is the implementation of the above approach:

## CPP

`// C++ implemenataion of the above approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// find the largest square ` `void` `findLargestSquare(` `long` `long` `int` `points[][2], ` `int` `n) ` `{ ` ` ` `// map to store which points exist ` ` ` `map<pair<` `long` `long` `int` `, ` `long` `long` `int` `>, ` `int` `> m; ` ` ` ` ` `// mark the available points ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `m[make_pair(points[i][0], points[i][1])]++; ` ` ` `} ` ` ` `long` `long` `int` `side = -1, x = -1, y = -1; ` ` ` ` ` `// a nested loop to choose the opposite corners of square ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` ` ` `// remove the chosen point ` ` ` `m[make_pair(points[i][0], points[i][1])]--; ` ` ` `for` `(` `int` `j = 0; j < n; j++) { ` ` ` ` ` `// remove the chosen point ` ` ` `m[make_pair(points[j][0], points[j][1])]--; ` ` ` ` ` `// check if the other two points exist ` ` ` `if` `(i != j ` ` ` `&& (points[i][0]-points[j][0]) == (points[i][1]-points[j][1])){ ` ` ` `if` `(m[make_pair(points[i][0], points[j][1])] > 0 ` ` ` `&& m[make_pair(points[j][0], points[i][1])] > 0) { ` ` ` ` ` `// if the square is largest then store it ` ` ` `if` `(side < ` `abs` `(points[i][0] - points[j][0]) ` ` ` `|| (side == ` `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 = ` `abs` `(points[i][0] - points[j][0]); ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` ` ` `// add the removed point ` ` ` `m[make_pair(points[j][0], points[j][1])]++; ` ` ` `} ` ` ` ` ` `// add the removed point ` ` ` `m[make_pair(points[i][0], points[i][1])]++; ` ` ` `} ` ` ` ` ` `// display the largest square ` ` ` `if` `(side != -1) ` ` ` `cout << ` `"Side of the square is : "` `<< side ` ` ` `<< ` `", \npoints of the square are "` `<< x << ` `", "` `<< y ` ` ` `<< ` `" "` ` ` `<< (x + side) << ` `", "` `<< y ` ` ` `<< ` `" "` ` ` `<< (x) << ` `", "` `<< (y + side) ` ` ` `<< ` `" "` ` ` `<< (x + side) << ` `", "` `<< (y + side) << endl; ` ` ` `else` ` ` `cout << ` `"No such square"` `<< endl; ` `} ` ` ` `//Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 6; ` ` ` ` ` `// given points ` ` ` `long` `long` `int` `points[n][2] ` ` ` `= { { 1, 1 }, { 4, 4 }, { 3, 4 }, { 4, 3 }, { 1, 4 }, { 4, 1 } }; ` ` ` ` ` `// find the largest square ` ` ` `findLargestSquare(points, n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implemenataion of the above approach ` ` ` `# find the largest square ` `def` `findLargestSquare(points,n): ` ` ` ` ` `# map to store which points exist ` ` ` `m ` `=` `dict` `() ` ` ` ` ` `# mark the available points ` ` ` `for` `i ` `in` `range` `(n): ` ` ` `m[(points[i][` `0` `], points[i][` `1` `])] ` `=` `\ ` ` ` `m.get((points[i][` `0` `], points[i][` `1` `]), ` `0` `) ` `+` `1` ` ` ` ` `side ` `=` `-` `1` ` ` `x ` `=` `-` `1` ` ` `y ` `=` `-` `1` ` ` ` ` `# a nested loop to choose the opposite corners of square ` ` ` `for` `i ` `in` `range` `(n): ` ` ` ` ` `# remove the chosen point ` ` ` `m[(points[i][` `0` `], points[i][` `1` `])]` `-` `=` `1` ` ` `for` `j ` `in` `range` `(n): ` ` ` ` ` `# remove the chosen point ` ` ` `m[(points[j][` `0` `], points[j][` `1` `])]` `-` `=` `1` ` ` ` ` `# check if the other two points exist ` ` ` `if` `(i !` `=` `j ` `and` `(points[i][` `0` `]` `-` `points[j][` `0` `]) ` `=` `=` `\ ` ` ` `(points[i][` `1` `]` `-` `points[j][` `1` `])): ` ` ` `if` `(m[(points[i][` `0` `], points[j][` `1` `])] > ` `0` `and` ` ` `m[(points[j][` `0` `], points[i][` `1` `])] > ` `0` `): ` ` ` ` ` `# if the square is largest then store it ` ` ` `if` `(side < ` `abs` `(points[i][` `0` `] ` `-` `points[j][` `0` `]) ` ` ` `or` `(side ` `=` `=` `abs` `(points[i][` `0` `] ` `-` `points[j][` `0` `]) ` ` ` `and` `((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 ` `=` `abs` `(points[i][` `0` `] ` `-` `points[j][` `0` `]) ` ` ` ` ` `# add the removed point ` ` ` `m[(points[j][` `0` `], points[j][` `1` `])] ` `+` `=` `1` ` ` ` ` `# add the removed point ` ` ` `m[(points[i][` `0` `], points[i][` `1` `])] ` `+` `=` `1` ` ` ` ` `# display the largest square ` ` ` `if` `(side !` `=` `-` `1` `): ` ` ` `print` `(` `"Side of the square is : "` `,side ` ` ` `,` `", \npoints of the square are "` `,x,` `", "` `,y ` ` ` `,` `" "` ` ` `,(x ` `+` `side),` `", "` `,y ` ` ` `,` `" "` ` ` `,(x),` `", "` `,(y ` `+` `side) ` ` ` `,` `" "` ` ` `,(x ` `+` `side),` `", "` `,(y ` `+` `side)) ` ` ` `else` `: ` ` ` `print` `(` `"No such square"` `) ` ` ` `# Driver code ` `n ` `=` `6` ` ` `# given points ` `points` `=` `[[ ` `1` `, ` `1` `],[ ` `4` `, ` `4` `],[ ` `3` `, ` `4` `],[ ` `4` `, ` `3` `],[ ` `1` `, ` `4` `],[ ` `4` `, ` `1` `] ] ` ` ` `# find the largest square ` `findLargestSquare(points, n) ` ` ` `# This code is contributed by mohit kumar 29 ` |

*chevron_right*

*filter_none*

**Output:**

Side of the square is : 3, points of the square are 1, 1 4, 1 1, 4 4, 4

** Time Complexity: **O(N^2)

## Recommended Posts:

- How to check if given four points form a square
- Maximum Squares possible parallel to both axes from N distinct points
- Count of Right-Angled Triangle formed from given N points whose base or perpendicular are parallel to X or Y axis
- Number of triangles formed by joining vertices of n-sided polygon with two common sides and no common sides
- Number of ways to arrange 2*N persons on the two sides of a table with X and Y persons on opposite sides
- Find the equation of plane which passes through two points and parallel to a given axis
- Check if given four integers (or sides) make rectangle
- Find whether only two parallel lines contain all coordinates points or not
- Probability of cutting a rope into three pieces such that the sides form a triangle
- Find sub-arrays from given two arrays such that they have equal sum
- Program to check if the points are parallel to X axis or Y axis
- Count of Squares that are parallel to the coordinate axis from the given set of N points
- Number of lines from given N points not parallel to X or Y axis
- Count of integers of length N and value less than K such that they contain digits only from the given set
- Find the area of the shaded region formed by the intersection of four semicircles in a square
- Check whether four points make a parallelogram
- Largest number by which given 3 numbers should be divided such that they leaves same remainder
- Find the number of ways to divide number into four parts such that a = c and b = d
- Find smallest perfect square number A such that N + A is also a perfect square number
- Smallest N digit number whose sum of square of digits is a Perfect Square

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.