# Find the coordinate that does not belong to any square

• Last Updated : 15 Jun, 2022

Given an array arr[] consisting of (4 * N + 1) pairs of coordinates representing the coordinates of the corners of any N squares such that only one coordinate doesn’t belong to any square, the task is to find that coordinate that doesn’t belong to any square.

Examples:

Input: N = 2, arr[] = { {0, 0}, {0, 1}, {0, 2}, {1, 0}, {1, 1}, {1, 2}, {2, 0}, {2, 1}, {2, 2} }
Output: 1 1
Explanation:
The square has four sides: x = 0, x = 2, y = 0, y = 2, now all the points belong to the square except one point (1, 1).

Input: N = 2, arr[] = { {0, 0}, {0, 1}, {0, 2}, {1, 0}, {0, 3}, {1, 2}, {2, 0}, {2, 1}, {2, 2} }
Output: 0 3

Approach: The given problem can be solved based on the following observations:

• The coordinates of the sides of the square will appear at least two times because N â‰¥ 2. Therefore, since there is only one point not on the boundary, the maximum between x-coordinates which appear at least twice will give us the x-coordinate of the right side of the square.
• The other three sides can be obtained similarly with different combinations of maximum/minimum and x-/ycoordinates.
• After knowing the sides of the square, it is easy to identify the point not on the boundary.

Follow the steps below to solve the problem :

• Iterate over a range [0, N] using the variable i and perform the following steps:
• Initialize the variables x1, y1 with 2e9 and x2, y2 with -2e9 to store the points of the upper and lower boundary of the square.
• Iterate over a range [0, N] using the variable j and perform the following steps:
• If i is not equal to j, then, perform the following steps:
• Set x1 to the minimum of x1 or p[j].first.
• Set x2 to the maximum of x2 or p[j].first.
• Set y1 to the minimum of y1  or p[j].second.
• Set y2 to the minimum of y2 or p[j].second.
• Initialize a variable, say ok to true and variables cnt1, cnt2, cnt3, cnt4 as 0 to store the count of points with maximum and minimum as x1, x2, y1, y2.
• Iterate over a range [0, N] using the variable j and perform the following steps:
• If i is not equal to j, then, perform the following steps:
• If p[j].first is equal to x1, then, increase the value of cnt1 by 1.
• If p[j].first is equal to x2, then, increase the value of cnt2 by 1.
• If p[j].second is equal to y1, then, increase the value of cnt3 by 1.
• If p[j].second is equal to y2, then, increase the value of cnt4 by 1.
• Else, set the value of ok to false.
• If the value of ok is true and the values of cnt1, cnt2, cnt3, cnt4 are greater than equal to N, and x2 – x2 is equal to y2 – y1, then, p[i] is the required point. Print the answer.

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach``#include ``using` `namespace` `std;` `typedef` `long` `long` `ll;``#define fi first``#define se second` `// Function to find the point that is``// not a part of the side of a square``void` `findPoint(``int` `n,``               ``vector > p)``{``    ``// Traverse each pair of coordinates``    ``for` `(``int` `i = 0; i < n * 4 + 1; ++i) {` `        ``int` `x1 = 2e9, x2 = -2e9;``        ``int` `y1 = 2e9, y2 = -2e9;` `        ``for` `(``int` `j = 0; j < n * 4 + 1; ++j)``            ``if` `(i != j) {` `                ``// Minimize x-coordinate``                ``// in all the points``                ``// except current point``                ``x1 = min(x1, p[j].fi);` `                ``// Maximize x-coordinate in``                ``// all the points except``                ``// the current point``                ``x2 = max(x2, p[j].fi);` `                ``// Minimize y-coordinate``                ``// in all the points``                ``// except current point``                ``y1 = min(y1, p[j].se);` `                ``// Maximize y-coordinate``                ``// in all the points``                ``// except current point``                ``y2 = max(y2, p[j].se);``            ``}` `        ``bool` `ok = 1;` `        ``int` `c1 = 0, c2 = 0;``        ``int` `c3 = 0, c4 = 0;` `        ``for` `(``int` `j = 1; j < n * 4 + 1; ++j)``            ``if` `(i != j) {` `                ``// If x-coordinate matches``                ``// with other same line``                ``if` `((p[j].fi == x1``                     ``|| p[j].fi == x2)``                    ``|| ((p[j].se == y1``                         ``|| p[j].se == y2))) {` `                    ``if` `(p[j].fi == x1)``                        ``++c1;` `                    ``if` `(p[j].fi == x2)``                        ``++c2;` `                    ``// If y coordinate matches``                    ``// with other same line``                    ``if` `(p[j].se == y1)``                        ``++c3;` `                    ``if` `(p[j].se == y2)``                        ``++c4;``                ``}``                ``else``                    ``ok = 0;``            ``}` `        ``// Check if the condition``        ``// for square exists or not``        ``if` `(ok && c1 >= n && c2 >= n``            ``&& c3 >= n && c4 >= n``            ``&& x2 - x1 == y2 - y1) {` `            ``// Print the output``            ``cout << p[i].fi << ``" "``                 ``<< p[i].se << ``"\n"``;``        ``}``    ``}``}` `// Driver Code``int` `main()``{``    ``int` `N = 2;``    ``vector > arr``        ``= { { 0, 0 }, { 0, 1 }, { 0, 2 },``            ``{ 1, 0 }, { 1, 1 }, { 1, 2 },``            ``{ 2, 0 }, { 2, 1 }, { 2, 2 } };` `    ``findPoint(N, arr);` `    ``return` `0;``}`

## Java

 `import` `java.util.ArrayList;` `//Java program for above approach``class` `GFG{``    ``static` `class` `pair{``        ``T fi;``        ``V se;``        ``pair(T a, V b){``            ``this``.fi = a;``            ``this``.se = b;``        ``}``    ``}` `    ``// Function to find the point that is``    ``// not a part of the side of a square``    ``static` `void` `findPoint(``int` `n,``                   ``ArrayList > p)``    ``{``        ``// Traverse each pair of coordinates``        ``for` `(``int` `i = ``0``; i < n * ``4` `+ ``1``; ++i) {` `            ``int` `x1 = (``int``) 2e9, x2 = (``int``) -2e9;``            ``int` `y1 = (``int``) 2e9, y2 = (``int``) -2e9;` `            ``for` `(``int` `j = ``0``; j < n * ``4` `+ ``1``; ++j)``                ``if` `(i != j) {` `                    ``// Minimize x-coordinate``                    ``// in all the points``                    ``// except current point``                    ``x1 = Math.min(x1, p.get(j).fi);` `                    ``// Maximize x-coordinate in``                    ``// all the points except``                    ``// the current point``                    ``x2 = Math.max(x2, p.get(j).fi);` `                    ``// Minimize y-coordinate``                    ``// in all the points``                    ``// except current point``                    ``y1 = Math.min(y1, p.get(j).se);` `                    ``// Maximize y-coordinate``                    ``// in all the points``                    ``// except current point``                    ``y2 = Math.max(y2, p.get(j).se);``                ``}` `            ``boolean` `ok = ``true``;` `            ``int` `c1 = ``0``, c2 = ``0``;``            ``int` `c3 = ``0``, c4 = ``0``;` `            ``for` `(``int` `j = ``1``; j < n * ``4` `+ ``1``; ++j)``                ``if` `(i != j) {` `                    ``// If x-coordinate matches``                    ``// with other same line``                    ``if` `((p.get(j).fi == x1``                            ``|| p.get(j).fi == x2)``                            ``|| ((p.get(j).se == y1``                            ``|| p.get(j).se == y2))) {` `                        ``if` `(p.get(j).fi == x1)``                            ``++c1;` `                        ``if` `(p.get(j).fi == x2)``                            ``++c2;` `                        ``// If y coordinate matches``                        ``// with other same line``                        ``if` `(p.get(j).se == y1)``                            ``++c3;` `                        ``if` `(p.get(j).se == y2)``                            ``++c4;``                    ``}``                    ``else``                        ``ok = ``false``;``                ``}` `            ``// Check if the condition``            ``// for square exists or not``            ``if` `(ok && c1 >= n && c2 >= n``                    ``&& c3 >= n && c4 >= n``                    ``&& x2 - x1 == y2 - y1) {` `                ``// Print the output``                ``System.out.println(p.get(i).fi + ``" "` `+ p.get(i).se);` `            ``}``        ``}``    ``}` `    ``//Driver Code``    ``public` `static` `void` `main(String[] args) {``        ``int` `N = ``2``;``        ``ArrayList > arr = ``new` `ArrayList<>();``        ``arr.add(``new` `pair(``0``,``0``));``        ``arr.add(``new` `pair(``0``,``1``));``        ``arr.add(``new` `pair(``0``,``2``));``        ``arr.add(``new` `pair(``1``,``0``));``        ``arr.add(``new` `pair(``1``,``1``));``        ``arr.add(``new` `pair(``1``,``2``));``        ``arr.add(``new` `pair(``2``,``0``));``        ``arr.add(``new` `pair(``2``,``1``));``        ``arr.add(``new` `pair(``2``,``2``));``        ``findPoint(N, arr);` `    ``}``}` `// This code is contributed by hritikrommie.`

## Python3

 `# Python 3 program for the above approach` `# Function to find the point that is``# not a part of the side of a square``def` `findPoint(n, p):``    ``# Traverse each pair of coordinates``    ``for` `i ``in` `range``(n ``*` `4` `+` `1``):``        ``x1 ``=` `2e9``        ``x2 ``=` `-``2e9``        ``y1 ``=` `2e9``        ``y2 ``=` `-``2e9` `        ``for` `j ``in` `range``(n ``*` `4` `+` `1``):``            ``if` `(i !``=` `j):` `                ``# Minimize x-coordinate``                ``# in all the points``                ``# except current point``                ``x1 ``=` `min``(x1, p[j][``0``])` `                ``# Maximize x-coordinate in``                ``# all the points except``                ``# the current point``                ``x2 ``=` `max``(x2, p[j][``0``])` `                ``# Minimize y-coordinate``                ``# in all the points``                ``# except current point``                ``y1 ``=` `min``(y1, p[j][``1``])` `                ``# Maximize y-coordinate``                ``# in all the points``                ``# except current point``                ``y2 ``=` `max``(y2, p[j][``1``])` `        ``ok ``=` `1` `        ``c1 ``=` `0``        ``c2 ``=` `0``        ``c3 ``=` `0``        ``c4 ``=` `0` `        ``for` `j ``in` `range``(``1``,n ``*` `4` `+` `1``,``1``):``            ``if` `(i !``=` `j):` `                ``# If x-coordinate matches``                ``# with other same line``                ``if` `((p[j][``0``] ``=``=` `x1 ``or` `p[j][``0``] ``=``=` `x2) ``or` `(p[j][``1``] ``=``=` `y1 ``or` `p[j][``1``] ``=``=` `y2)):``                    ``if` `(p[j][``0``] ``=``=` `x1):``                        ``c1 ``+``=` `1` `                    ``if` `(p[j][``0``] ``=``=` `x2):``                        ``c2 ``+``=` `1` `                    ``# If y coordinate matches``                    ``# with other same line``                    ``if` `(p[j][``1``] ``=``=` `y1):``                        ``c3 ``+``=` `1` `                    ``if` `(p[j][``1``] ``=``=` `y2):``                        ``c4 ``+``=` `1``                ``else``:``                    ``ok ``=` `0` `        ``# Check if the condition``        ``# for square exists or not``        ``if` `(ok ``and` `c1 >``=` `n ``and` `c2 >``=` `n ``and` `c3 >``=` `n ``and` `c4 >``=` `n ``and` `x2 ``-` `x1 ``=``=` `y2 ``-` `y1):` `            ``# Print the output``            ``print``(p[i][``0``],p[i][``1``])``        ` `# Driver Code``if` `__name__ ``=``=` `'__main__'``:``    ``N ``=` `2``    ``arr ``=`  `[[``0``, ``0``],[``0``, ``1``],[``0``, ``2``],[``1``, ``0``],[``1``, ``1``],[``1``, ``2``],[``2``, ``0``],[``2``, ``1``],[``2``, ``2``]]` `    ``findPoint(N, arr)``    ` `    ``# This code is contributed by ipg2016107.`

## Javascript

 `// JavaScript program for the above approach` `// Function to find the point that is``// not a part of the side of a square``function` `findPoint(n, p)``{``    ``// Traverse each pair of coordinates``    ``for` `(let i = 0; i < n * 4 + 1; ++i) {` `        ``let x1 = 2e9, x2 = -2e9;``        ``let y1 = 2e9, y2 = -2e9;` `        ``for` `(let j = 0; j < n * 4 + 1; ++j){``            ``if` `(i != j) {` `                ``// Minimize x-coordinate``                ``// in all the points``                ``// except current point``                ``x1 = Math.min(x1, p[j][0]);` `                ``// Maximize x-coordinate in``                ``// all the points except``                ``// the current point``                ``x2 = Math.max(x2, p[j][0]);` `                ``// Minimize y-coordinate``                ``// in all the points``                ``// except current point``                ``y1 = Math.min(y1, p[j][1]);` `                ``// Maximize y-coordinate``                ``// in all the points``                ``// except current point``                ``y2 = Math.max(y2, p[j][1]);``            ``}``        ``}` `        ``let ok = 1;` `        ``let c1 = 0, c2 = 0;``        ``let c3 = 0, c4 = 0;` `        ``for` `(let j = 1; j < n * 4 + 1; ++j){``            ``if` `(i != j) {` `                ``// If x-coordinate matches``                ``// with other same line``                ``if` `((p[j][0] == x1 || p[j][0] == x2) || (p[j][1] == y1 || p[j][1] == y2)) {` `                    ``if` `(p[j][0] == x1)``                        ``++c1;` `                    ``if` `(p[j][0] == x2)``                        ``++c2;` `                    ``// If y coordinate matches``                    ``// with other same line``                    ``if` `(p[j][1] == y1)``                        ``++c3;` `                    ``if` `(p[j][1] == y2)``                        ``++c4;``                ``}``                ``else``{``                    ``ok = 0;   ``                ``}``            ``} ``        ``}` `        ``// Check if the condition``        ``// for square exists or not``        ``if` `(ok && c1 >= n && c2 >= n && c3 >= n && c4 >= n && x2 - x1 == y2 - y1) {``            ``// Print the output``            ``console.log(p[i][0] + ``" "` `+ p[i][1]);``        ``}``    ``}``}` `// Driver Code` `let N = 2;``let arr =  [[0, 0], [0, 1], [0, 2],``        ``[1, 0], [1, 1], [1, 2 ],``        ``[2, 0], [2, 1], [2, 2]];` `findPoint(N, arr);` `// The code is contributed by Gautam goel(gautamgoel962)`

Output:

`1 1`

Time Complexity: O(N2)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up