VOOZH about

URL: https://www.geeksforgeeks.org/dsa/find-the-coordinate-that-does-not-belong-to-any-square/

⇱ Find the coordinate that does not belong to any square - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Find the coordinate that does not belong to any square

Last Updated : 23 Jul, 2025

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:

 
 


Output: 
1 1

 


 

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


 

Comment
Article Tags: