Given N points in a plane in the form of 2D array such that every row consist of two integer L and R where L belongs to x-ordinate and R belongs to y-ordinate. The task is to count the triplets of points(say a, b & c) such that distance between a & b is equals to the distance between a & c.
Note: The order of triplets matters.
Examples:
Input: arr[] = { { 0, 0 }, { 1, 0 }, { 2, 0 } }
Output: 2
Explanation:
The possible triplets are: {{1, 0}, {0, 0}, {2, 0}} and {{1, 0}, {2, 0}, {0, 0}}
Input: arr[] = { {1, 0}, {1, -1}, {2, 3}, {4, 3}, {4, 4} }
Output: 0
Explanation:
There is no such triplets exists.
Approach:
- For each point calculate it’s distance to every other points.
- Store intermediate distances(say d) for point to other points in a Map.
- If Map has already same distance then count of triplets is twice the value stored for d in Map.
- Update the count of current distance in the Map.
Below is the implementation of the above:
C++
#include <bits/stdc++.h>
using namespace std;
int countTriplets(vector<vector< int > >& p)
{
int count = 0;
for ( int i = 0; i < p.size(); i++) {
unordered_map< int , int > d;
for ( int j = 0; j < p.size(); j++) {
int dist = pow (p[j][1] - p[i][1], 2)
+ pow (p[j][0] - p[i][0], 2);
if (d[dist] > 0) {
count += 2 * d[dist];
}
d[dist]++;
}
}
return count;
}
int main()
{
vector<vector< int > > arr = { { 0, 0 },
{ 1, 0 },
{ 2, 0 } };
cout << countTriplets(arr);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int countTriplets( int p[][])
{
int count = 0 ;
for ( int i = 0 ; i < p.length; i++) {
HashMap<Integer, Integer> d = new HashMap<Integer,Integer>();
for ( int j = 0 ; j < p.length; j++) {
int dist = ( int )(Math.pow(p[j][ 1 ] - p[i][ 1 ], 2 )+ Math.pow(p[j][ 0 ] - p[i][ 0 ], 2 ));
if (d.containsKey(dist) && d.get(dist) > 0 ) {
count += 2 * d.get(dist);
}
if (d.containsKey(dist)){
d.put(dist,d.get(dist)+ 1 );
}
else
d.put(dist, 1 );
}
}
return count;
}
public static void main(String args[])
{
int arr[][] = { { 0 , 0 },
{ 1 , 0 },
{ 2 , 0 } };
System.out.println(countTriplets(arr));
}
}
|
Python3
def countTriplets(p) :
count = 0 ;
for i in range ( len (p)) :
d = {};
for j in range ( len (p)) :
dist = pow (p[j][ 1 ] - p[i][ 1 ], 2 ) + \
pow (p[j][ 0 ] - p[i][ 0 ], 2 );
if dist not in d :
d[dist] = 0 ;
if (d[dist] > 0 ) :
count + = 2 * d[dist];
d[dist] + = 1 ;
return count;
if __name__ = = "__main__" :
arr = [ [ 0 , 0 ],
[ 1 , 0 ],
[ 2 , 0 ] ];
print (countTriplets(arr));
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int countTriplets( int [,] p)
{
int count = 0;
for ( int i = 0; i < p.GetLength(0); i++)
{
Dictionary< int ,
int > d = new Dictionary< int ,
int >();
for ( int j = 0; j < p.GetLength(0); j++)
{
int dist = ( int )(Math.Pow(p[j, 1] -
p[i, 1], 2) +
Math.Pow(p[j, 0] -
p[i, 0], 2));
if (d.ContainsKey(dist) && d[dist] > 0)
{
count += 2 * d[dist];
}
if (d.ContainsKey(dist))
{
d[dist]++;
}
else
d.Add(dist, 1);
}
}
return count;
}
static void Main()
{
int [,] arr = new int [3, 2]{ { 0, 0 },
{ 1, 0 },
{ 2, 0 } };
Console.WriteLine(countTriplets(arr));
}
}
|
Javascript
<script>
function countTriplets(p)
{
let count = 0;
for (let i = 0; i < p.length; i++) {
let d = new Map();
for (let j = 0; j < p.length; j++) {
let dist = (Math.pow(p[j][1] - p[i][1], 2)+ Math.pow(p[j][0] - p[i][0], 2));
if (d.has(dist) && d.get(dist) > 0) {
count += 2 * d.get(dist);
}
if (d.has(dist)){
d.set(dist,d.get(dist)+1);
}
else
d.set(dist,1);
}
}
return count;
}
let arr = [[ 0, 0 ],
[ 1, 0 ],
[ 2, 0 ]];
document.write(countTriplets(arr));
</script>
|
Last Updated :
07 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...