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

Given **N** points in 2 dimensional space. The task is to count the number of triplets pairs **(A, B, C)** such that point **B** is the midpoint of line segment formed by joining **points A and C**.

**Examples:**

Input:points = {{1, 1}, {2, 2}, {3, 3}}

Output:1

The point (2, 2) is the midpoint of the line segment joining points (1, 1) and (3, 3).

Input:points = {{1, 1}, {1, 2}, {1, 5}}

Output:0

**Approach:** Consider a pair of points **A** and **C**. The midpoint of the line segment joining these points will be **((A * X + C * X) / 2, (A * Y + C * Y) / 2))**. If the point is present in the given list of points, we have found a triplet. To quickly check if a point is in our list of points we can use a set. Doing this for all pairs of points will give us the required count.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the count of possible triplets ` `int` `countTriplets(` `int` `n, vector<pair<` `int` `, ` `int` `> > points) ` `{ ` ` ` `set<pair<` `int` `, ` `int` `> > pts; ` ` ` `int` `ct = 0; ` ` ` ` ` `// Insert all the points in a set ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `pts.insert(points[i]); ` ` ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `for` `(` `int` `j = i + 1; j < n; j++) { ` ` ` `int` `x = points[i].first + points[j].first; ` ` ` `int` `y = points[i].second + points[j].second; ` ` ` ` ` `// If the mid point exists in the set ` ` ` `if` `(x % 2 == 0 && y % 2 == 0) ` ` ` `if` `(pts.find(make_pair(x / 2, y / 2)) ` ` ` `!= pts.end()) ` ` ` `ct++; ` ` ` `} ` ` ` ` ` `// Return the count of valid triplets ` ` ` `return` `ct; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `vector<pair<` `int` `, ` `int` `> > points ` ` ` `= { { 1, 1 }, { 2, 2 }, { 3, 3 } }; ` ` ` `int` `n = points.size(); ` ` ` `cout << countTriplets(n, points); ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` ` ` `# Function to return the count ` `# of possible triplets ` `def` `countTriplets(n, points) : ` ` ` ` ` `pts ` `=` `[] ` ` ` `ct ` `=` `0` `; ` ` ` ` ` `# Insert all the points in a set ` ` ` `for` `i ` `in` `range` `(n) : ` ` ` `pts.append(points[i]); ` ` ` ` ` `for` `i ` `in` `range` `(n) : ` ` ` `for` `j ` `in` `range` `(i ` `+` `1` `, n) : ` ` ` `x ` `=` `points[i][` `0` `] ` `+` `points[j][` `0` `]; ` ` ` `y ` `=` `points[i][` `1` `] ` `+` `points[j][` `1` `]; ` ` ` ` ` `# If the mid point exists in the set ` ` ` `if` `(x ` `%` `2` `=` `=` `0` `and` `y ` `%` `2` `=` `=` `0` `) : ` ` ` `if` `[x ` `/` `/` `2` `, y ` `/` `/` `2` `] ` `in` `pts : ` ` ` `ct ` `+` `=` `1` ` ` ` ` `# Return the count of valid triplets ` ` ` `return` `ct ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `points ` `=` `[[ ` `1` `, ` `1` `], [ ` `2` `, ` `2` `], [ ` `3` `, ` `3` `]] ` ` ` `n ` `=` `len` `(points) ` ` ` `print` `(countTriplets(n, points)) ` ` ` `# This code is contributed by Ryuga ` |

*chevron_right*

*filter_none*

## PHP

**Output:**

1

## Recommended Posts:

- Pairs from an array that satisfy the given condition
- Count all possible N digit numbers that satisfy the given condition
- Count Distinct Non-Negative Integer Pairs (x, y) that Satisfy the Inequality x*x + y*y < n
- Count maximum points on same line
- Count Integral points inside a Triangle
- Count distinct points visited on the number line
- Count Fibonacci numbers in given range in O(Log n) time and O(1) space
- Count pairs with Odd XOR
- Count pairs (i,j) such that (i+j) is divisible by A and B both
- Count of pairs (x, y) in an array such that x < y
- Count of pairs of (i, j) such that ((n % i) % j) % n is maximized
- Count number of pairs (A <= N, B <= N) such that gcd (A , B) is B
- Count pairs (a, b) whose sum of cubes is N (a^3 + b^3 = N)
- Count number of pairs (i, j) such that arr[i] * arr[j] > arr[i] + arr[j]
- Count of pairs from 1 to a and 1 to b whose sum is divisible by N

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.