- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

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

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

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.

#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); }

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 Questions & Answers
- 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++
- Find all pairs (a,b) and (c,d) in array which satisfy ab = cd in C++
- Count pairs (a, b) whose sum of squares is N (a^2 + b^2 = N) in C++
- Count number of triplets (a, b, c) such that a^2 + b^2 = c^2 and 1<=a<=b<=c<= n in C++
- Find largest d in array such that a + b + c = d in C++
- Find four elements a, b, c and d in an array such that a+b = c+d in C++
- Count number of pairs (A <= N, B <= N) such that gcd (A , B) is B in C++
- How to count the number of values that satisfy a condition in an R vector?
- Count of obtuse angles in a circle with ‘k' equidistant points between 2 given points in C++
- Find 2^(2^A) % B in C++
- Program to find number of subsequences that satisfy the given sum condition using Python
- Find n positive integers that satisfy the given equations in C++

Advertisements