# Calculate the Manhattan Distance between two cells of given 2D array

Given a 2D array of size **M * N** and two points in the form **(X _{1}, Y_{1})** and

**(X**where

_{2}, Y_{2})**X**and

_{1}**X**represents the rows and

_{2}**Y**and

_{1}**Y**represents the column. The task is to calculate the

_{2}**Manhattan distance**between the given points.

**Examples:**

Input:M = 5, N = 5, X_{1}= 1, Y_{1}= 2, X_{2}= 3, Y_{2}= 3Output:3Explanation:As per the definition, the Manhattan the distance is same as sum of the absolute difference of the coordinates.

Input:M = 5, N = 5, X_{1}= 4, Y_{1}= 2, X_{2}= 4, Y_{2}= 2Output:0

**Approach:** The approach is based on mathematical observation. The **Manhattan distance** between two points is the sum of absolute difference of the coordinates.

Manhattan distance = |X_{1}– X_{2}| + |Y_{1}– Y_{2}|

Below is the implementation of the above approach.

## C++

`// C++ code to implement above approach` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Code to calculate Manhattan distance` `int` `manhattanDist(` `int` `M, ` `int` `N, ` `int` `X1,` ` ` `int` `Y1, ` `int` `X2, ` `int` `Y2)` `{` ` ` `int` `dist = ` `abs` `(X2 - X1) + ` `abs` `(Y2 - Y1);` ` ` `return` `dist;` `}` `// Driver code` `int` `main()` `{` ` ` `// Define size of 2-D array` ` ` `int` `M = 5, N = 5;` ` ` `// First point` ` ` `int` `X1 = 1, Y1 = 2;` ` ` `// Second point` ` ` `int` `X2 = 3, Y2 = 3;` ` ` `cout << manhattanDist(M, N, X1, Y1, X2, Y2);` ` ` `return` `0;` `}` |

## Java

`// java code to implement above approach` `class` `GFG` `{` ` ` `// Code to calculate Manhattan distance` ` ` `static` `int` `manhattanDist(` `int` `M, ` `int` `N, ` `int` `X1,` ` ` `int` `Y1, ` `int` `X2, ` `int` `Y2) {` ` ` `int` `dist = Math.abs(X2 - X1) + Math.abs(Y2 - Y1);` ` ` `return` `dist;` ` ` `}` ` ` `// Driver code` ` ` `public` `static` `void` `main(String args[])` ` ` `{` ` ` `// Define size of 2-D array` ` ` `int` `M = ` `5` `, N = ` `5` `;` ` ` `// First point` ` ` `int` `X1 = ` `1` `, Y1 = ` `2` `;` ` ` `// Second point` ` ` `int` `X2 = ` `3` `, Y2 = ` `3` `;` ` ` `System.out.println(manhattanDist(M, N, X1, Y1, X2, Y2));` ` ` `}` `}` `// This code is contributed by gfgking.` |

## Python3

`# Python code for the above approach` `import` `math as Math` `# Code to calculate Manhattan distance` `def` `manhattanDist(M, N, X1, Y1, X2, Y2):` ` ` `dist ` `=` `Math.fabs(X2 ` `-` `X1) ` `+` `Math.fabs(Y2 ` `-` `Y1)` ` ` `return` `(` `int` `)(dist)` `# Driver code` `# Define size of 2-D array` `M ` `=` `5` `N ` `=` `5` `# First point` `X1 ` `=` `1` `Y1 ` `=` `2` `# Second point` `X2 ` `=` `3` `Y2 ` `=` `3` `print` `(manhattanDist(M, N, X1, Y1, X2, Y2))` `# This code is contributed by Saurabh Jaiswal` |

## C#

`// C# code to implement above approach` `using` `System;` `class` `GFG {` ` ` `// Code to calculate Manhattan distance` ` ` `static` `int` `manhattanDist(` `int` `M, ` `int` `N, ` `int` `X1, ` `int` `Y1,` ` ` `int` `X2, ` `int` `Y2)` ` ` `{` ` ` `int` `dist = Math.Abs(X2 - X1) + Math.Abs(Y2 - Y1);` ` ` `return` `dist;` ` ` `}` ` ` `// Driver code` ` ` `public` `static` `void` `Main()` ` ` `{` ` ` `// Define size of 2-D array` ` ` `int` `M = 5, N = 5;` ` ` `// First point` ` ` `int` `X1 = 1, Y1 = 2;` ` ` `// Second point` ` ` `int` `X2 = 3, Y2 = 3;` ` ` `Console.WriteLine(` ` ` `manhattanDist(M, N, X1, Y1, X2, Y2));` ` ` `}` `}` `// This code is contributed by ukasp.` |

## Javascript

`<script>` ` ` `// JavaScript code for the above approach` ` ` `// Code to calculate Manhattan distance` ` ` `function` `manhattanDist(M, N, X1,` ` ` `Y1, X2, Y2) {` ` ` `let dist = Math.abs(X2 - X1) + Math.abs(Y2 - Y1);` ` ` `return` `dist;` ` ` `}` ` ` `// Driver code` ` ` `// Define size of 2-D array` ` ` `let M = 5, N = 5;` ` ` `// First point` ` ` `let X1 = 1, Y1 = 2;` ` ` `// Second point` ` ` `let X2 = 3, Y2 = 3;` ` ` `document.write(manhattanDist(M, N, X1, Y1, X2, Y2));` ` ` `// This code is contributed by Potta Lokesh` ` ` `</script>` |

**Output**

3

* Time Complexity:* O(1)

*O(1)*

**Auxiliary Space:**