Given an array arr containing N points, the task is to find the total number of triplets in which the points are equidistant.
A triplet of points (P1, P2, P3) is said to be equidistant when the distance between P1 and P2 is the same as of the distance between P1 and P3.
Note: The order of the points matters, i.e., (P1, P2, P3) is different from (P2, P3, P1).
Example:
Input: arr = [[0, 0], [1, 0], [2, 0]]
Output: 2
Explanation:
Since the order of the points matters, we have two different sets of points [[1, 0], [0, 0], [2, 0]] and [[1, 0], [2, 0], [0, 0]] in which the points are equidistant.
Input: arr = [[1, 1], [1, 3], [2, 0]]
Output: 0
Explanation:
It is not possible to get any such triplet in which the points are equidistant.
Approach: To solve the problem mentioned above, we know that the order of a triplet matter, so there could be more than one permutations of the same triplet satisfying the condition for an equidistant pair of points.
- First, we will compute all the permutations of a triplet which has equidistant points in it.
- Repeat this same process for every different triplet of points in the list. To calculate the distance we will use the square of the distance between the respective coordinates.
- Use hashmap to store the various number of equidistant pairs of points for a single triplet.
- As soon as we count the total number of pairs, we calculate the required permutation. We repeat this process for all different triplets and add all the permutations to our result.
Below is the implementation to the above approach:
// C++ implementation to Find // the total number of Triplets // in which the points are Equidistant #include <bits/stdc++.h> using namespace std; // function to count such triplets int numTrip(vector<pair<int, int> >& points) { int res = 0; // Iterate over all the points for (int i = 0; i < points.size(); ++i) { unordered_map<long, int> map(points.size()); // Iterate over all points other // than the current point for (int j = 0; j < points.size(); ++j) { if (j == i) continue; int dy = points[i].second - points[j].second; int dx = points[i].first - points[j].first; // Compute squared euclidean distance // for the current point int key = dy * dy; key += dx * dx; map[key]++; } for (auto& p : map) // Compute nP2 that is n * (n - 1) res += p.second * (p.second - 1); } // Return the final result return res; } // Driver code int main() { vector<pair<int, int> > mat = { { 0, 0 }, { 1, 0 }, { 2, 0 } }; cout << numTrip(mat); return 0; } |
2
Time Complexity: O(N2)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Number of cells in matrix which are equidistant from given two points
- Count of obtuse angles in a circle with 'k' equidistant points between 2 given points
- Count of different straight lines with total n points with m collinear
- Minimum number of points to be removed to get remaining points on one side of axis
- Prime points (Points that split a number into two primes)
- Number of Integral Points between Two Points
- Count number of triplets with product equal to given number with duplicates allowed
- Count number of triplets with product equal to given number
- Count number of triplets with product equal to given number | Set 2
- Count number of triplets (a, b, c) such that a^2 + b^2 = c^2 and 1 <= a <= b <= c <= n
- Number of unique triplets whose XOR is zero
- Count triplets such that sum of any two number is equal to third | Set 2
- Find number of triplets in array such that a[i]>a[j]>a[k] and i<j<k
- Number of triplets such that each value is less than N and each pair sum is a multiple of K
- Number of triplets in array having subarray xor equal
- Find the total number of composite factor for a given number
- Maximum number of unique Triplets such that each element is selected only once
- Steps required to visit M points in order on a circular ring of N points
- Ways to choose three points with distance between the most distant points <= L
- Number of quadrilaterals possible from the given points
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.

