# Pairs with same Manhattan and Euclidean distance

In a given Cartesian plane, there are N points. The task is to find the Number of Pairs of points(A, B) such that

- Point A and Point B do not coincide.
- Manhattan Distance and the Euclidean Distance between the points should be equal.

**Note:** Pair of 2 points(A, B) is considered same as Pair of 2 points(B, A).

Manhattan Distance =

|x2-x1|+|y2-y1|Euclidean Distance =

((x2-x1)^2 + (y2-y1)^2)^0.5where points are (x1, y1) and (x2, y2).

**Examples:**

Input:N = 3, Points = {{1, 2}, {2, 3}, {1, 3}}

Output:2

Pairs are:

1) (1, 2) and (1, 3)

Euclidean distance of (1, 2) and (1, 3) = &root;((1 – 1)^{2}+ (3 – 2)^{2}) = 1

Manhattan distance of (1, 2) and (1, 3) = |(1 – 1)| + |(2 – 3)| = 12) (1, 3) and (2, 3)

Euclidean distance of (1, 3) and (2, 3) = &root;((1 – 2)^{2}+ (3 – 3)^{2}) = 1

Manhattan distance of (1, 3) and (2, 3) = |(1 – 2)| + |(3 – 3)| = 1

Input:N = 3, Points = { {1, 1}, {2, 3}, {1, 1} }

Output:0

Here none of the pairs satisfy the above two conditions

**Approach:** On solving the equation

|x2-x1|+|y2-y1| = sqrt((x2-x1)^2+(y2-y1)^2)

we get , **x2 = x1 or y2 = y1.**

Consider 3 maps,

1) Map X, where X[x_{i}] stores the number of points having their x-coordinate equal to x_{i}

2) Map Y, where Y[y_{i}] stores the number of points having their y-coordinate equal to y_{i}

3) Map XY, where XY[(X_{i}, Y_{i})] stores the number of points coincident with point (x_{i}, y_{i})

Now,

Let Xans be the Number of pairs with same X-coordinates = ^{X[xi]}_{2} for all distinct x_{i} =

Let Yans be the Number of pairs with same Y-coordinates = ^{Y[xi]}_{2} for all distinct y_{i}

Let XYans be the Number of coincident points = ^{XY[{xi, yi}]}_{2} for all distinct points (x_{i}, y_{i})

Thus the required answer = **Xans + Yans – XYans**

Below is the implementation of the above approach:

## C++

`// C++ implementtaion of the above approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the number of non coincident ` `// pairs of points with manhattan distance ` `// equal to euclidean distance ` `int` `findManhattanEuclidPair(pair<` `int` `, ` `int` `> arr[], ` `int` `n) ` `{ ` ` ` `// To store frequency of all distinct Xi ` ` ` `map<` `int` `, ` `int` `> X; ` ` ` ` ` `// To store Frequency of all distinct Yi ` ` ` `map<` `int` `, ` `int` `> Y; ` ` ` ` ` `// To store Frequency of all distinct ` ` ` `// points (Xi, Yi); ` ` ` `map<pair<` `int` `, ` `int` `>, ` `int` `> XY; ` ` ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `int` `xi = arr[i].first; ` ` ` `int` `yi = arr[i].second; ` ` ` ` ` `// Hash xi coordinate ` ` ` `X[xi]++; ` ` ` ` ` `// Hash yi coordinate ` ` ` `Y[yi]++; ` ` ` ` ` `// Hash the point (xi, yi) ` ` ` `XY[arr[i]]++; ` ` ` `} ` ` ` ` ` `int` `xAns = 0, yAns = 0, xyAns = 0; ` ` ` ` ` `// find pairs with same Xi ` ` ` `for` `(` `auto` `xCoordinatePair : X) { ` ` ` `int` `xFrequency = xCoordinatePair.second; ` ` ` ` ` `// calculate ((xFrequency) C2) ` ` ` `int` `sameXPairs = ` ` ` `(xFrequency * (xFrequency - 1)) / 2; ` ` ` `xAns += sameXPairs; ` ` ` `} ` ` ` ` ` `// find pairs with same Yi ` ` ` `for` `(` `auto` `yCoordinatePair : Y) { ` ` ` `int` `yFrequency = yCoordinatePair.second; ` ` ` ` ` `// calculate ((yFrequency) C2) ` ` ` `int` `sameYPairs = ` ` ` `(yFrequency * (yFrequency - 1)) / 2; ` ` ` `yAns += sameYPairs; ` ` ` `} ` ` ` ` ` `// find pairs with same (Xi, Yi) ` ` ` `for` `(` `auto` `XYPair : XY) { ` ` ` `int` `xyFrequency = XYPair.second; ` ` ` ` ` `// calculate ((xyFrequency) C2) ` ` ` `int` `samePointPairs = ` ` ` `(xyFrequency * (xyFrequency - 1)) / 2; ` ` ` `xyAns += samePointPairs; ` ` ` `} ` ` ` ` ` `return` `(xAns + yAns - xyAns); ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `pair<` `int` `, ` `int` `> arr[] = { ` ` ` `{ 1, 2 }, ` ` ` `{ 2, 3 }, ` ` ` `{ 1, 3 } ` ` ` `}; ` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` ` ` `cout << findManhattanEuclidPair(arr, n) << endl; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

# Python3 implementtaion of the

# above approach

from collections import defaultdict

# Function to return the number of

# non coincident pairs of points with

# manhattan distance equal to

# euclidean distance

def findManhattanEuclidPair(arr, n):

# To store frequency of all distinct Xi

X = defaultdict(lambda:0)

# To store Frequency of all distinct Yi

Y = defaultdict(lambda:0)

# To store Frequency of all distinct

# points (Xi, Yi)

XY = defaultdict(lambda:0)

for i in range(0, n):

xi = arr[i][0]

yi = arr[i][1]

# Hash xi coordinate

X[xi] += 1

# Hash yi coordinate

Y[yi] += 1

# Hash the point (xi, yi)

XY[tuple(arr[i])] += 1

xAns, yAns, xyAns = 0, 0, 0

# find pairs with same Xi

for xCoordinatePair in X:

xFrequency = X[xCoordinatePair]

# calculate ((xFrequency) C2)

sameXPairs = (xFrequency *

(xFrequency – 1)) // 2

xAns += sameXPairs

# find pairs with same Yi

for yCoordinatePair in Y:

yFrequency = Y[yCoordinatePair]

# calculate ((yFrequency) C2)

sameYPairs = (yFrequency *

(yFrequency – 1)) // 2

yAns += sameYPairs

# find pairs with same (Xi, Yi)

for XYPair in XY:

xyFrequency = XY[XYPair]

# calculate ((xyFrequency) C2)

samePointPairs = (xyFrequency *

(xyFrequency – 1)) // 2

xyAns += samePointPairs

return (xAns + yAns – xyAns)

# Driver Code

if __name__ == “__main__”:

arr = [[1, 2], [2, 3], [1, 3]]

n = len(arr)

print(findManhattanEuclidPair(arr, n))

# This code is contributed by Rituraj Jain

**Output:**

2

**Time Complexity**: O(NlogN), where N is the number of points

**Space Complexity**: O(N)

## Recommended Posts:

- Count paths with distance equal to Manhattan distance
- Find the number of distinct pairs of vertices which have a distance of exactly k in a tree
- Find a point such that sum of the Manhattan distances is minimized
- Euclidean algorithms (Basic and Extended)
- C Program for Basic Euclidean algorithms
- Find HCF of two numbers without using recursion or Euclidean algorithm
- Ways to form n/2 pairs such that difference of pairs is minimum
- Given an array of pairs, find all symmetric pairs in it
- Distance between a point and a Plane in 3 D
- Pair with given sum and maximum shortest distance from end
- Closest perfect square and its distance
- Program to calculate distance between two points
- Program for distance between two points on earth
- Perpendicular distance between a point and a Line in 2 D
- Minimum distance between two occurrences of maximum

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.