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 intermidiate 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++

`// C++ program for the above appproach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to count the triplets ` `int` `countTriplets(vector<vector<` `int` `> >& 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<vector<` `int` `> > arr = { { 0, 0 }, ` ` ` `{ 1, 0 }, ` ` ` `{ 2, 0 } }; ` ` ` ` ` `// Function call ` ` ` `cout << countTriplets(arr); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## 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<Integer, Integer> d = ` `new` `HashMap<Integer,Integer>(); ` ` ` ` ` `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 ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

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

## Recommended Posts:

- Count of triplets in an Array such that A[i] * A[j] = A[k] and i < j < k
- Count triplets in an array such that i<j<k and a[j] - a[i] = a[k] - a[j] = D
- Count triplets such that sum of any two number is equal to third | Set 2
- Count of triplets from the given Array such that sum of any two elements is the third element
- Count triplets in a sorted doubly linked list whose product is equal to a given value x
- Count number of triplets with product equal to given number
- All unique triplets that sum up to a given value
- Find all triplets with zero sum
- Print all triplets with given sum
- Total number of triplets (A, B, C) in which the points B and C are Equidistant to A
- Print all triplets in sorted array that form AP
- Count of numbers whose difference with Fibonacci count upto them is atleast K
- Longest subarray having count of 1s one more than count of 0s
- Count of quadruplets with given Sum | Set 2
- Count pairs with given sum
- Count all pairs with given XOR
- Count subarrays with same even and odd elements
- Count of unique pairs (arr[i], arr[j]) such that i < j
- Longest substring with count of 1s more than 0s
- Count of all sub-strings with sum of weights at most K

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.