Missing vertex among N axis-parallel rectangles
Last Updated :
25 Oct, 2022
Given N axis-parallel rectangles in a 2-D Cartesian coordinate system and coordinates of 4N-1 vertices, the task is to find the single vertex missing. Examples:
Input: N = 2, V[][] = {{1, 1}, {1, 2}, {4, 6}, {2, 1}, {9, 6}, {9, 3}, {4, 3}}
Output: {2, 2}
Explanation:
The coordinates forming an axis parallel rectangle are {4, 6}, {9, 6}, {4, 3}, {9, 3}.
For the remaining coordinates to form a rectangle {1, 1}, {1, 2}, {2, 1}, the missing coordinate is {2, 2}
Input: N = 3, V[][] = {{3, 8}, {0, 0}, {4, 6}, {0, 2}, {9, 6}, {9, 3}, {4, 3}, {6, 4}, {1, 0}, {3, 4}, {6, 8}}
Output: {1, 2}
Approach:
Follow the steps below to solve the problem:
- Store the frequencies x-coordinates and y-coordinates in a Map.
- Iterate over the Map to find an element with odd frequency for both the co-ordinates.
- Finally, print the x and y coordinate with odd frequency.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void MissingPoint(vector<pair< int , int > > V,
int N)
{
map< int , int > mp;
for ( int i = 0; i < V.size(); i++) {
mp[V[i].first]++;
}
int x, y;
for ( auto it : mp) {
if (it.second % 2 == 1) {
x = it.first;
break ;
}
}
mp.clear();
for ( int i = 0; i < V.size(); i++) {
mp[V[i].second]++;
}
for ( auto it : mp) {
if (it.second % 2 == 1) {
y = it.first;
break ;
}
}
cout << x << " " << y;
}
int main()
{
int N = 2;
vector<pair< int , int > > V;
V.push_back({ 1, 1 });
V.push_back({ 1, 2 });
V.push_back({ 4, 6 });
V.push_back({ 2, 1 });
V.push_back({ 9, 6 });
V.push_back({ 9, 3 });
V.push_back({ 4, 3 });
MissingPoint(V, N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static class pair
{
int first, second;
public pair( int first, int second)
{
this .first = first;
this .second = second;
}
}
static void MissingPoint(Vector<pair> V, int N)
{
HashMap<Integer,
Integer> mp = new HashMap<Integer,
Integer>();
for ( int i = 0 ; i < V.size(); i++)
{
if (mp.containsKey(V.get(i).first))
mp.put(V.get(i).first,
V.get(i).first + 1 );
else
mp.put(V.get(i).first, 1 );
}
int x = 0 , y = 0 ;
for (Map.Entry<Integer,
Integer> it : mp.entrySet())
{
if (it.getValue() % 2 == 1 )
{
x = it.getKey();
break ;
}
}
mp.clear();
for ( int i = 0 ; i < V.size(); i++)
{
if (mp.containsKey(V.get(i).second))
mp.put(V.get(i).second,
V.get(i).second + 1 );
else
mp.put(V.get(i).second, 1 );
}
for (Map.Entry<Integer,
Integer> it : mp.entrySet())
{
if (it.getValue() % 2 == 1 )
{
y = it.getKey();
break ;
}
}
System.out.print(x + " " + y);
}
public static void main(String[] args)
{
int N = 2 ;
Vector<pair> V = new Vector<pair>();
V.add( new pair( 1 , 1 ));
V.add( new pair( 1 , 2 ));
V.add( new pair( 4 , 6 ));
V.add( new pair( 2 , 1 ));
V.add( new pair( 9 , 6 ));
V.add( new pair( 9 , 3 ));
V.add( new pair( 4 , 3 ));
MissingPoint(V, N);
}
}
|
Python3
from collections import defaultdict
def MissingPoint(V, N):
mp = defaultdict( lambda : 0 )
for i in range ( len (V)):
mp[V[i][ 0 ]] + = 1
for it in mp.keys():
if (mp[it] % 2 = = 1 ):
x = it
break
del mp
mp = defaultdict( lambda : 0 )
for i in range ( len (V)):
mp[V[i][ 1 ]] + = 1
for it in mp.keys():
if (mp[it] % 2 = = 1 ):
y = it
break
print (x, y)
if __name__ = = '__main__' :
N = 2
V = []
V.append([ 1 , 1 ])
V.append([ 1 , 2 ])
V.append([ 4 , 6 ])
V.append([ 2 , 1 ])
V.append([ 9 , 6 ])
V.append([ 9 , 3 ])
V.append([ 4 , 3 ])
MissingPoint(V, N)
|
C#
using System;
using System.Collections.Generic;
class GFG{
class pair
{
public int first, second;
public pair( int first, int second)
{
this .first = first;
this .second = second;
}
}
static void MissingPoint(List<pair> V,
int N)
{
Dictionary< int ,
int > mp = new Dictionary< int ,
int >();
for ( int i = 0; i < V.Count; i++)
{
if (mp.ContainsKey(V[i].first))
mp[V[i].first] = mp[V[i].first] + 1;
else
mp.Add(V[i].first, 1);
}
int x = 0, y = 0;
foreach (KeyValuePair< int ,
int > it in mp)
{
if (it.Value % 2 == 1)
{
x = it.Key;
break ;
}
}
mp.Clear();
for ( int i = 0; i < V.Count; i++)
{
if (mp.ContainsKey(V[i].second))
mp[V[i].second] = mp[V[i].second] + 1;
else
mp.Add(V[i].second, 1);
}
foreach (KeyValuePair< int ,
int > it in mp)
{
if (it.Value % 2 == 1)
{
y = it.Key;
break ;
}
}
Console.Write(x + " " + y);
}
public static void Main(String[] args)
{
int N = 2;
List<pair> V = new List<pair>();
V.Add( new pair(1, 1));
V.Add( new pair(1, 2));
V.Add( new pair(4, 6));
V.Add( new pair(2, 1));
V.Add( new pair(9, 6));
V.Add( new pair(9, 3));
V.Add( new pair(4, 3));
MissingPoint(V, N);
}
}
|
Javascript
function MissingPoint(V, N)
{
let mp = new Map();
for (let i = 0; i < V.length; i++) {
if (mp.has(V[i][0])){
mp.set(V[i][0], mp.get(V[i][0]) + 1);
}
else {
mp.set(V[i][0], 1);
}
}
let x, y;
for (const [key, value] of mp.entries()) {
if (value % 2 == 1) {
x = key;
break ;
}
}
mp.clear();
for (let i = 0; i < V.length; i++) {
if (mp.has(V[i][1])){
mp.set(V[i][1], mp.get(V[i][1]) + 1);
}
else {
mp.set(V[i][1], 1);
}
}
for (const [key, value] of mp) {
if (value % 2 == 1) {
y = key;
break ;
}
}
console.log(x, y);
}
let N = 2;
let V = new Array();
V.push([1, 1]);
V.push([1, 2]);
V.push([4, 6]);
V.push([2, 1]);
V.push([9, 6]);
V.push([9, 3]);
V.push([4, 3]);
MissingPoint(V, N);
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Alternative Approach with no extra space:
Since the edges of the rectangle are axis parallel, the x and y co-ordinate of all vertices should occur even number of (at least Two) times.
Eg. For the given sample containing {{1, 1}, {1, 2}, {4, 6}, {2, 1}, {9, 6}, {9, 3}, {4, 3}} as input, the array of x co-ordinates of all the points is {1, 1, 4, 2, 9, 9, 4}.
and the array of y co-ordinates of all points is {1, 2, 6, 1, 6, 3, 3}. Here we can see that all the co-ordinate value occurs twice (even number of times) except for the missing vertex.
As XOR of any number with itself gives 0. This is valid for any even number of occurrence of that number. XOR of any number with itself that occurs odd number of times will lead to the number itself.
Since the co-ordinates of the missing vertex occur odd number of times, we can use the XOR concept to find out that vertex in O(1) space complexity.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void MissingPoint(vector<pair< int , int > > V,
int N)
{
int x = 0, y = 0;
for ( int i = 0; i < V.size(); i++) {
x = x ^ V[i].first;
y = y ^ V[i].second;
}
cout << x << " " << y;
}
int main()
{
int N = 2;
vector<pair< int , int > > V;
V.push_back({ 1, 1 });
V.push_back({ 1, 2 });
V.push_back({ 4, 6 });
V.push_back({ 2, 1 });
V.push_back({ 9, 6 });
V.push_back({ 9, 3 });
V.push_back({ 4, 3 });
MissingPoint(V, N);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static void MissingPoint( int [][] V,
int N)
{
int x = 0 , y = 0 ;
for ( int i = 0 ; i < V.length; i++) {
x = x ^ V[i][ 0 ];
y = y ^ V[i][ 1 ];
}
System.out.print(x + " " + y);
}
public static void main(String[] args)
{
int N = 2 ;
int [][] V = new int [][] { { 1 , 1 }, { 1 , 2 }, { 4 , 6 }, { 2 , 1 }, { 9 , 6 }, { 9 , 3 }, { 4 , 3 }};
MissingPoint(V, N);
}
}
|
Python3
def MissingPoint(arr, n):
missing_x = 0
missing_y = 0
for i in arr:
missing_x = missing_x ^ i[ 0 ]
missing_y = missing_y ^ i[ 1 ]
print (missing_x, missing_y)
if __name__ = = "__main__" :
n = 2
points = [[ 1 , 1 ], [ 1 , 2 ], [ 4 , 6 ], [ 2 , 1 ], [ 9 , 6 ], [ 9 , 3 ], [ 4 , 3 ]]
MissingPoint(points, n)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static void MissingPoint(List< int []> V,
int N)
{
int x = 0, y = 0;
for ( int i = 0; i < V.Count; i++) {
x = x ^ V[i][0];
y = y ^ V[i][1];
}
Console.Write(x + " " + y);
}
public static void Main( string [] args)
{
int N = 2;
List< int []> V = new List< int []>();
V.Add( new [] {1, 1 });
V.Add( new [] {1, 2 });
V.Add( new [] {4, 6 });
V.Add( new [] {2, 1 });
V.Add( new [] {9, 6 });
V.Add( new [] {9, 3 });
V.Add( new [] {4, 3 });
MissingPoint(V, N);
}
}
|
Javascript
function MissingPoint(V, N)
{
let x = 0, y = 0;
for (let i = 0; i < V.length; i++) {
x = x ^ V[i][0];
y = y ^ V[i][1];
}
process.stdout.write(x + " " + y);
}
let N = 2;
let V = [];
V.push([ 1, 1 ]);
V.push([ 1, 2 ]);
V.push([ 4, 6 ]);
V.push([ 2, 1 ]);
V.push([ 9, 6 ]);
V.push([ 9, 3 ]);
V.push([ 4, 3 ]);
MissingPoint(V, N);
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...