Missing a Point | PTMSSNG | CodeChef

Chef has N axis-parallel rectangles in a 2D Cartesian coordinate system. These rectangles may intersect, but it is guaranteed that all their 4N vertices are pairwise distinct.
Unfortunately, Chef lost one vertex, and up until now, none of his fixes have worked (although putting an image of a point on a milk carton might not have been the greatest idea after all…). Therefore, he gave you the task of finding it! You are given the remaining 4N1 points and you should find the missing one.

Input

  • The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows.
  • The first line of each test case contains a single integer N.
  • Then, 4N1 lines follow. Each of these lines contains two space-separated integers x and y denoting a vertex (x,y) of some rectangle.

Output


For each test case, print a single line containing two space-separated integers X and Y ― the coordinates of the missing point. It can be proved that the missing point can be determined uniquely.

Constraints

  • T100
  • 1N2105
  • |x|,|y|109
  • the sum of N over all test cases does not exceed 2105

Subtasks

Subtask #1 (20 points):
  • T=5
  • N20
Subtask #2 (30 points): |x|,|y|105
Subtask #3 (50 points): original constraints

The original set of points are:
fig1
Upon adding the missing point (2,2)N=2 rectangles can be formed:
fig1

Solution (C++)


#include <bits/stdc++.h>
using namespace std;
int main() {
// your code goes here
int t;
cin>>t;
while(t--)
{
    int n;
    cin>>n;
    vector<int>vec_x,vec_y;
    for(int i=0; i<(4*n)-1;i++)
    {
        int x,y;
        cin>>x>>y;
        vec_x.push_back(x);
        vec_y.push_back(y);
    }
    sort(vec_x.begin(),vec_x.end());
    sort(vec_y.begin(),vec_y.end());
    int ansa=0,ansb=0;
   // find_it(vec_x,vec_y);
    for(int i=1;i<=vec_x.size();i+=2)
    {
        if(vec_x[i]!=vec_x[i-1])
        {
            cout<<vec_x[i-1]<<" ";
            break;
        }
    }
    for(int i=1;i<=vec_y.size();i+=2)
    {
        if(vec_y[i]!=vec_y[i-1])
         {
             cout<<vec_y[i-1]<<"\n";
             break;
         }
    }
   // cout<<ansa<<" "<<ansb<<endl;
}
return 0;
}

Comments

Popular posts from this blog

Boxes through a Tunnel | HackerRank

Array Manipulation | HackerRank

String Validators | HackerRank