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 numbers whose difference with Fibonacci count upto them is atleast K
- Count all subarrays whose sum can be split as difference of squares of two Integers
- Count of Fibonacci divisors of a given number
- Count of prime digits of a Number which divides the number
- Array range queries to count the number of Fibonacci numbers with updates
- Count nodes in the given tree whose weight is a fibonacci number
- Count of distinct XORs formed by rearranging two Binary strings
- Count numbers divisible by K in a range with Fibonacci digit sum for Q queries
- Minimize prize count required such that smaller value gets less prize in an adjacent pair
- How to count all child elements of a particular element using JavaScript ?
- Generate an array of given size with equal count and sum of odd and even numbers
- Queries for the count of even digit sum elements in the given range using Segment Tree.
- Count of Nodes in a LinkedList whose value is equal to their frequency
- Count of N digit palindromic numbers divisible by 9
- Count subarrays with sum as difference of squares of two numbers
- Maximize count of 0s in left and 1s in right substring by splitting given Binary string
- Count elements whose sum with K is greater than max element
- Count of leaf nodes of the tree whose weighted string is a palindrome
- Count the number of Prime Cliques in an undirected graph
- Count of Nodes which has Prime Digit sum weight in a Tree

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.