Total number of triplets (A, B, C) in which the points B and C are Equidistant to A
Last Updated :
09 Nov, 2021
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++
#include <bits/stdc++.h>
using namespace std;
int numTrip(vector<pair< int , int > >& points)
{
int res = 0;
for ( int i = 0; i < points.size(); ++i) {
unordered_map< long , int >
map(points.size());
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;
int key = dy * dy;
key += dx * dx;
map[key]++;
}
for ( auto & p : map)
res += p.second * (p.second - 1);
}
return res;
}
int main()
{
vector<pair< int , int > > mat
= { { 0, 0 }, { 1, 0 }, { 2, 0 } };
cout << numTrip(mat);
return 0;
}
|
Java
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;
}
}
static int numTrip(ArrayList points)
{
int res = 0 ;
for ( int i = 0 ; i < points.size(); ++i)
{
HashMap<Long,
Integer> map = new HashMap<>();
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;
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())
res += p * (p - 1 );
}
return res;
}
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));
}
}
|
Python3
def numTrip(points):
res = 0
for i in range ( len (points)):
map = {}
for j in range ( len (points)):
if (j = = i):
continue
dy = points[i][ 1 ] - points[j][ 1 ]
dx = points[i][ 0 ] - points[j][ 0 ]
key = dy * dy
key + = dx * dx
map [key] = map .get(key, 0 ) + 1
for p in map :
res + = map [p] * ( map [p] - 1 )
return res
if __name__ = = '__main__' :
mat = [ [ 0 , 0 ],
[ 1 , 0 ],
[ 2 , 0 ] ]
print (numTrip(mat))
|
C#
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;
}
}
static int numTrip(ArrayList points)
{
int res = 0;
for ( int i = 0; i < points.Count; ++i)
{
Dictionary< long ,
int > map = new Dictionary< long ,
int >();
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;
int key = dy * dy;
key += dx * dx;
if (map.ContainsKey(key))
{
map[key]++;
}
else
{
map[key] = 1;
}
}
foreach ( int p in map.Values)
res += p * (p - 1);
}
return res;
}
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));
}
}
|
Javascript
<script>
function numTrip(points)
{
let res = 0;
for (let i = 0; i < points.length; ++i)
{
let map = new Map();
for (let j = 0; j < points.length; ++j)
{
if (j == i)
continue ;
let dy = points[i][1] -
points[j][1];
let dx = points[i][0] -
points[j][0];
let key = dy * dy;
key += dx * dx;
if (map.has(key))
{
map.set(key, map.get(key) + 1);
}
else
{
map.set(key, 1);
}
}
for (let [key, value] of map.entries())
res += value * (value - 1);
}
return res;
}
let mat = [];
mat.push([0, 0]);
mat.push([1, 0]);
mat.push([2, 0]);
document.write(numTrip(mat));
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...