# Count of Triplets

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.

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach:

1. For each point calculate it’s distance to every other points.
2. Store intermidiate distances(say d) for point to other points in a Map.
3. If Map has already same distance then count of triplets is twice the value stored for d in Map.
4. Update the count of current distance in the Map.

Below is the implementation of the above:

## C++

 `// C++ program for the above appproach ` `#include ` `using` `namespace` `std; ` ` `  `// Function to count the triplets ` `int` `countTriplets(vector >& p) ` `{ ` ` `  `    ``// Intialise count ` `    ``int` `count = 0; ` ` `  `    ``// Traverse the arr[] ` `    ``for` `(``int` `i = 0; i < p.size(); i++) { ` ` `  `        ``// Map to store the distance between ` `        ``// every pairs p[i] and p[j] ` `        ``unordered_map<``int``, ``int``> d; ` ` `  `        ``for` `(``int` `j = 0; j < p.size(); j++) { ` ` `  `            ``// Find the distance ` `            ``int` `dist = ``pow``(p[j][1] - p[i][1], 2) ` `                       ``+ ``pow``(p[j][0] - p[i][0], 2); ` ` `  `            ``// If count of distance is greater ` `            ``// than 0, then find the count ` `            ``if` `(d[dist] > 0) { ` `                ``count += 2 * d[dist]; ` `            ``} ` ` `  `            ``// Update the current count of the ` `            ``// distance ` `            ``d[dist]++; ` `        ``} ` `    ``} ` ` `  `    ``// Return the count of triplets ` `    ``return` `count; ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` ` `  `    ``// Set of points in plane ` `    ``vector > arr = { { 0, 0 }, ` `                                 ``{ 1, 0 }, ` `                                 ``{ 2, 0 } }; ` ` `  `    ``// Function call ` `    ``cout << countTriplets(arr); ` `    ``return` `0; ` `} `

## Java

 `// Java program for the above appproach ` `import` `java.util.*; ` `class` `GFG{ ` ` `  `    ``// Function to count the triplets ` `    ``static` `int` `countTriplets(``int` `p[][]) ` `    ``{ ` `     `  `        ``// Intialise count ` `        ``int` `count = ``0``; ` `     `  `        ``// Traverse the arr[] ` `        ``for` `(``int` `i = ``0``; i < p.length; i++) { ` `     `  `            ``// Map to store the distance between ` `            ``// every pairs p[i] and p[j] ` `            ``HashMap d = ``new` `HashMap(); ` `     `  `            ``for` `(``int` `j = ``0``; j < p.length; j++) { ` `     `  `                ``// Find the distance ` `                ``int` `dist = (``int``)(Math.pow(p[j][``1``] - p[i][``1``], ``2``)+ Math.pow(p[j][``0``] - p[i][``0``], ``2``)); ` `     `  `                ``// If count of distance is greater ` `                ``// than 0, then find the count ` `                 `  `                ``if` `(d.containsKey(dist) && d.get(dist) > ``0``) { ` `                    ``count += ``2` `* d.get(dist); ` `                ``} ` `     `  `                ``// Update the current count of the ` `                ``// distance ` `                ``if` `(d.containsKey(dist)){ ` `                    ``d.put(dist,d.get(dist)+``1``); ` `                ``} ` `                ``else` `                    ``d.put(dist,``1``); ` `            ``} ` `        ``} ` `     `  `        ``// Return the count of triplets ` `        ``return` `count; ` `    ``} ` `     `  `    ``// Driver Code ` `    ``public` `static` `void` `main(String args[]) ` `    ``{ ` `     `  `        ``// Set of points in plane ` `        ``int` `arr[][] = { { ``0``, ``0` `}, ` `                                    ``{ ``1``, ``0` `}, ` `                                    ``{ ``2``, ``0` `} }; ` `     `  `        ``// Function call ` `        ``System.out.println(countTriplets(arr)); ` `         `  `    ``} ` `} ` ` `  `// This code is contributed by AbhiThakur `

## Python3

 `# Python3 program for the above appproach ` ` `  `# Function to count the triplets ` `def` `countTriplets(p) : ` ` `  `    ``# Intialise count ` `    ``count ``=` `0``; ` ` `  `    ``# Traverse the arr[] ` `    ``for` `i ``in` `range``(``len``(p)) : ` ` `  `        ``# Map to store the distance between ` `        ``# every pairs p[i] and p[j] ` `        ``d ``=` `{}; ` ` `  `        ``for` `j ``in` `range``(``len``(p)) : ` ` `  `             `  `            ``# Find the distance ` `            ``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 count of distance is greater ` `            ``# than 0, then find the count ` `            ``if` `(d[dist] > ``0``) : ` `                ``count ``+``=` `2` `*` `d[dist]; ` ` `  `            ``# Update the current count of the ` `            ``# distance ` `            ``d[dist] ``+``=` `1``; ` `     `  `    ``# Return the count of triplets ` `    ``return` `count; ` ` `  `# Driver Code ` `if` `__name__ ``=``=` `"__main__"` `: ` ` `  `    ``# Set of points in plane ` `    ``arr ``=` `[ [ ``0``, ``0` `], ` `            ``[ ``1``, ``0` `], ` `            ``[ ``2``, ``0` `] ]; ` ` `  `    ``# Function call ` `    ``print``(countTriplets(arr)); ` `     `  `# This code is contributed by Yash_R `

Output:

```2
```

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.

Improved By : Yash_R, abhaysingh290895

Article Tags :
Practice Tags :

1

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.