Count triplet pairs (A, B, C) of points in 2-D space that satisfy the given condition in C++

C++Server Side ProgrammingProgramming

We are given an input of N points on a 2-D space. The goal is to find the count of triplets of points from the input such that one point is the mid-point on the line between the other two. i.e if triplet is (A,B,C) then B is midpoint of A and C ( or any other combination of A,B,C ).

We will do this by inserting all points as pairs <int,int> into a vector. Then add all pairs from this vector in set. By taking two points from the set check if the sum of (x,y) coordinates divided by 2 exist in the same set. If yes increment triplet count.

Let’s understand with examples.

Input 

{ 1,2 }, { 4,2} , { 2,1 } , { 7,2 } N=4 pairs

Output 

Count of triplet pairs that satisfy the given condition are: 1

Explanation 

Here {4,2} is mid-point between {1,2} and {7,2}. Only 1 such triplet

Input 

{ 1,2 }, { 4,2} , { 2,1 } , { 5,2 }, { 8,1} , {1,1} N=6

Output 

Count of triplet pairs that satisfy the given condition are: 1

Explanation 

No such triplet exist

Approach used in the below program is as follows

  • We are taking a vector of pairs of type <int,int>.

  • Each pair contains (x,y) coordinates.

  • Function mid_point(vector<pair<int, int>> vec, int size) takes a vector and it’s size as input and returns the number of triplets that satisfy the mid-point condition.

  • Take the initial variable count as 0 for such triplets.

  • Insert all pairs from the vector into a set<pair<int, int> > sets. It will have all unique points.

  • Traverse the vector using two for loops for each pair of points.

  • Store sum of x coordinates of both points in integer point_A and sum of y coordinates of both points in integer point_B.

  • If both these sums in point_A and point_B are even then check for mid-point condition.

  • If a pair(point_A/2,point_B/2) exists as a pair in the set means mid-point exists. Increment count of triplet.

  • In the end count will have number of triplets.

  • Return the count as a result at the end of the for loop.

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
int mid_point(vector<pair<int, int>> vec, int size){
   int count = 0;
   set<pair<int, int> > sets;
   for (int i = 0; i < size; i++){
      sets.insert(vec[i]);
   }
   for (int i = 0; i < size; i++){
      for (int j = i + 1; j < size; j++){
         int point_A = vec[i].first + vec[j].first;
         int point_B = vec[i].second + vec[j].second;
         if (point_A % 2 == 0 && point_B % 2 == 0){
            if (sets.find(make_pair(point_A / 2, point_B / 2)) != sets.end()){
               count++;
            }
         }
      }
   }
   return count;
}
int main(){
   vector<pair<int, int>> vec = { { 9, 2 }, { 5, 2 }, { 1, 2 } };
   int size = vec.size();
   cout<<"Count of triplet pairs (A, B, C) of points in 2-D space that satisfy the given condition are: "<<mid_point(vec, size);
}

Output

If we run the above code it will generate the following output −

Count of triplet pairs (A, B, C) of points in 2-D space that satisfy the given condition are: 1
raja
Published on 31-Oct-2020 05:38:12
Advertisements