Total number of triplets (A, B, C) in which the points B and C are Equidistant to A

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Output:

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.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.