Given 4N-1 vertices of N rectangles find the missing vertex.

You have been given N Casterian Coordinate axis-parallel rectangles, but one of its vertexes is missing, i.e. Out of 4N vertex, you have only been given 4N-1 vertices.  You need to find the missing vertex.

->The rectangles can overlap

->All the vertices are unique i.e no two vertex overlap

Example:

INPUT:

N=1

vertices = (2,2), (3,2), (2,3)

Output

The missing vertex is: (3,3)

Explanation: N=1 means we have been given just a single rectangle, the three vertices given to us are : (2,2), (3,2), (2,3). Plotting the points on the XY coordinate axes we can see that the missing vertex will be (3,3).

Approach:

A possible solution is by employing the use of HashSet. We create two HashSet: one for the x coordinates and the other for the y coordinates.

Now loop through the x_cordinates in the input vertices array, if the 'x' coordinate is already present in the HashSet then remove it from the HashSet or else insert it to the HashSet. 

Again loop through the y_cordinates in the input vertices array, if the 'y' coordinate is already present in the HashSet then remove it from the HashSet or else insert it to the HashSet. 

Visit me at: http://www.caffeine-coated.codes/

Comments