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:
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:
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.  

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation to find the total
// number of Triplets in which the
// points are Equidistant
import java.util.*;
 
@SuppressWarnings("unchecked")
class GFG{
  
static class pair
{
    public int first, second;
      
    public pair(int first, int second)
    {
        this.first = first;
        this.second = second;
    }
}
   
// Function to size() such triplets
static int numTrip(ArrayList points)
{
    int res = 0;
     
    // Iterate over all the points
    for(int i = 0; i < points.size(); ++i)
    {
         
        HashMap<Long,
                Integer> map = new HashMap<>();
        
        // Iterate over all points other
        // than the current point
        for(int j = 0; j < points.size(); ++j)
        {
            if (j == i)
                continue;
                  
            int dy = ((pair)points.get(i)).second -
                     ((pair)points.get(j)).second;
            int dx = ((pair)points.get(i)).first -
                     ((pair)points.get(j)).first;
   
            // Compute squared euclidean distance
            // for the current point
            long key = dy * dy;
            key += dx * dx;
              
            if (map.containsKey(key))
            {
                map.put(key, map.get(key) + 1);
            }
            else
            {
                map.put(key, 1);
            }
        }
          
        for(int p : map.values())
         
            // Compute nP2 that is n * (n - 1)
            res += p * (p - 1);
    }
     
    // Return the final result
    return res;
}
   
// Driver code
public static void main(String []args)
{
    ArrayList mat = new ArrayList();
    mat.add(new pair(0, 0));
    mat.add(new pair(1, 0));
    mat.add(new pair(2, 0));
     
    System.out.println(numTrip(mat));
}
}
 
// This code is contributed by rutvik_56
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation to find
# the total number of Triplets
# in which the points are Equidistant
  
# Function to count such triplets
def numTrip(points):
     
    res = 0
     
    # Iterate over all the points
    for i in range(len(points)):
        map = {}
         
        # Iterate over all points other
        # than the current point
        for j in range(len(points)):
            if (j == i):
                continue
             
            dy = points[i][1] - points[j][1]
            dx = points[i][0] - points[j][0]
  
            # Compute squared euclidean distance
            # for the current point
            key = dy * dy
            key += dx * dx
  
            map[key] = map.get(key, 0) + 1
  
        for p in map:
             
            # Compute nP2 that is n * (n - 1)
            res += map[p] * (map[p] - 1)
  
    # Return the final result
    return res
  
# Driver code
if __name__ == '__main__':
     
    mat = [ [ 0, 0 ],
            [ 1, 0 ],
            [ 2, 0 ] ]
  
    print (numTrip(mat))
  
# This code is contributed by mohit kumar 29
chevron_right

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
using System;
using System.Collections;
using System.Collections.Generic;
 
class GFG{
 
class pair
{
    public int first, second;
     
    public pair(int first, int second)
    {
        this.first = first;
        this.second = second;
    }
}
  
// Function to count such triplets
static int numTrip(ArrayList points)
{
    int res = 0;
     
    // Iterate over all the points
    for(int i = 0; i < points.Count; ++i)
    {
         
        Dictionary<long,
                   int> map = new Dictionary<long,
                                             int>();
       
        // Iterate over all points other
        // than the current point
        for(int j = 0; j < points.Count; ++j)
        {
            if (j == i)
                continue;
                 
            int dy = ((pair)points[i]).second -
                     ((pair)points[j]).second;
            int dx = ((pair)points[i]).first -
                     ((pair)points[j]).first;
  
            // Compute squared euclidean distance
            // for the current point
            int key = dy * dy;
            key += dx * dx;
             
            if (map.ContainsKey(key))
            {
                map[key]++;
            }
            else
            {
                map[key] = 1;
            }
        }
         
        foreach(int p in map.Values)
         
            // Compute nP2 that is n * (n - 1)
            res += p * (p - 1);
    }
  
    // Return the final result
    return res;
}
  
// Driver code
public static void Main(string []args)
{
    ArrayList mat = new ArrayList(){ new pair(0, 0),
                                     new pair(1, 0),
                                     new pair(2, 0) };
   
    Console.Write(numTrip(mat));
}
}
 
// This code is contributed by pratham76
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.




Article Tags :