- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

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

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

#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

- Related Articles
- Count index pairs which satisfy the given condition in C++
- Count subsets that satisfy the given condition in C++
- Find numbers a and b that satisfy the given condition in C++
- Count all possible N digit numbers that satisfy the given condition in C++
- C++ program to find out the number of pairs in an array that satisfy a given condition
- Find all pairs (a,b) and (c,d) in array which satisfy ab = cd in C++
- How to count the number of values that satisfy a condition in an R vector?
- Count pairs (a, b) whose sum of squares is N (a^2 + b^2 = N) in C++
- Count Distinct Non-Negative Integer Pairs (x, y) that Satisfy the Inequality x*x + y*y < n in C++
- Count Distinct Non-Negative Integer Pairs (x, y) that Satisfy the Inequality x*x + y*y < n in C++
- Count the pairs of vowels in the given string in C++
- Count pairs with given sum in C++
- Find a triplet that sum to a given value in C++
- Find n positive integers that satisfy the given equations in C++
- Count of obtuse angles in a circle with ‘k' equidistant points between 2 given points in C++