# Farthest cell from a given cell in a Matrix

Given the integers **N**, **M**, **R** and **C **where **N **and **M** denotes the number of rows and columns in a matrix and **(R, C)** denotes a cell in that matrix, the task is to find the distance of the farthest cell from the cell **(R, C)**. **Note: **The matrix can only be traversed either horizontally or vertically at a time.

**Examples:**

Input:N = 15, M = 12, R = 1, C = 6Output:20Explanation:The maximum distance calculated from all the four corners are 20, 5, 19, 6. Therefore, 20 is the required answer.

Input:N = 15, M = 12, R = 2, C = 4Output:21

**Naive Approach:** The simplest approach is to traverse the matrix and calculate the distance of each cell of the matrix from the given cell **(R, C)** and maintain the maximum of all distances.

**Time Complexity**: O(N*M), as we are using nested loops to traverse N*M times.

**Auxiliary Space**: O(1), as we are not using any extra space.

**Efficient Approach:** To optimize the above the approach, the observation is that the farthest distant cell from any cell in a matrix will be one of the four corner-most cells i.e. **(1, 1), (1, M), (N, 1), (N, M)**.

Follow the steps below to solve the problem:

- Initialize
**d1**,**d2**,**d3**and**d4**be equal to**N + M – R – C**,**R + C – 2**,**N – R + C – 1**and**M – C + R – 1**respectively. - Print the maximum among
**d1**,**d2**,**d3**and**d4**.

Below is the implementation of the above approach:

## C++

`// C++ program for the above approach` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Function to find the farthest` `// cell distance from the given cell` `void` `farthestCellDistance(` `int` `N, ` `int` `M,` ` ` `int` `R, ` `int` `C)` `{` ` ` `// Distance from all the` ` ` `// cornermost cells` ` ` `// From cell(N, M)` ` ` `int` `d1 = N + M - R - C;` ` ` `// From Cell(1, 1)` ` ` `int` `d2 = R + C - 2;` ` ` `// From cell(N, 1)` ` ` `int` `d3 = N - R + C - 1;` ` ` `// From cell(1, M)` ` ` `int` `d4 = M - C + R - 1;` ` ` `// Finding out maximum` ` ` `int` `maxDistance = max(d1,` ` ` `max(d2,` ` ` `max(d3, d4)));` ` ` `// Print the answer` ` ` `cout << maxDistance;` `}` `// Driver Code` `int` `main()` `{` ` ` `int` `N = 15, M = 12, R = 1, C = 6;` ` ` `farthestCellDistance(N, M, R, C);` ` ` `return` `0;` `}` |

## Java

`// Java program for the above approach` `import` `java.io.*;` `class` `GFG{` `// Function to find the farthest` `// cell distance from the given cell` `static` `void` `farthestCellDistance(` `int` `N, ` `int` `M,` ` ` `int` `R, ` `int` `C)` `{` ` ` ` ` `// Distance from all the` ` ` `// cornermost cells` ` ` `// From cell(N, M)` ` ` `int` `d1 = N + M - R - C;` ` ` `// From Cell(1, 1)` ` ` `int` `d2 = R + C - ` `2` `;` ` ` `// From cell(N, 1)` ` ` `int` `d3 = N - R + C - ` `1` `;` ` ` `// From cell(1, M)` ` ` `int` `d4 = M - C + R - ` `1` `;` ` ` `// Finding out maximum` ` ` `int` `maxDistance = Math.max(d1, Math.max(` ` ` `d2, Math.max(d3, d4)));` ` ` `// Print the answer` ` ` `System.out.println(maxDistance);` `}` `// Driver Code` `public` `static` `void` `main(String[] args)` `{` ` ` `int` `N = ` `15` `, M = ` `12` `, R = ` `1` `, C = ` `6` `;` ` ` ` ` `farthestCellDistance(N, M, R, C);` `}` `}` `// This code is contributed by Dharanendra L V` |

## Python3

`# Python program for the above approach` `# Function to find the farthest` `# cell distance from the given cell` `def` `farthestCellDistance(N, M, R, C):` ` ` ` ` `# Distance from all the` ` ` `# cornermost cells` ` ` `# From cell(N, M)` ` ` `d1 ` `=` `N ` `+` `M ` `-` `R ` `-` `C;` ` ` `# From Cell(1, 1)` ` ` `d2 ` `=` `R ` `+` `C ` `-` `2` `;` ` ` `# From cell(N, 1)` ` ` `d3 ` `=` `N ` `-` `R ` `+` `C ` `-` `1` `;` ` ` `# From cell(1, M)` ` ` `d4 ` `=` `M ` `-` `C ` `+` `R ` `-` `1` `;` ` ` `# Finding out maximum` ` ` `maxDistance ` `=` `max` `(d1, ` `max` `(d2, ` `max` `(d3, d4)));` ` ` `# Print the answer` ` ` `print` `(maxDistance);` `# Driver Code` `if` `__name__ ` `=` `=` `'__main__'` `:` ` ` `N ` `=` `15` `;` ` ` `M ` `=` `12` `;` ` ` `R ` `=` `1` `;` ` ` `C ` `=` `6` `;` ` ` `farthestCellDistance(N, M, R, C);` `# This code is contributed by shikhasingrajput` |

## C#

`// C# program for the above approach` `using` `System;` `class` `GFG{` `// Function to find the farthest` `// cell distance from the given cell` `static` `void` `farthestCellDistance(` `int` `N, ` `int` `M,` ` ` `int` `R, ` `int` `C)` `{` ` ` ` ` `// Distance from all the` ` ` `// cornermost cells` ` ` `// From cell(N, M)` ` ` `int` `d1 = N + M - R - C;` ` ` `// From Cell(1, 1)` ` ` `int` `d2 = R + C - 2;` ` ` `// From cell(N, 1)` ` ` `int` `d3 = N - R + C - 1;` ` ` `// From cell(1, M)` ` ` `int` `d4 = M - C + R - 1;` ` ` `// Finding out maximum` ` ` `int` `maxDistance = Math.Max(d1, Math.Max(` ` ` `d2, Math.Max(d3, d4)));` ` ` `// Print the answer` ` ` `Console.WriteLine(maxDistance);` `}` `// Driver Code` `static` `public` `void` `Main()` `{` ` ` `int` `N = 15, M = 12, R = 1, C = 6;` ` ` ` ` `farthestCellDistance(N, M, R, C);` `}` `}` `// This code is contributed by Dharanendra L V` |

## Javascript

`<script>` `// Java script program for the above approach` `// Function to find the farthest` `// cell distance from the given cell` `function` `farthestCellDistance( N, M,` ` ` `R, C)` `{` ` ` ` ` `// Distance from all the` ` ` `// cornermost cells` ` ` `// From cell(N, M)` ` ` `let d1 = N + M - R - C;` ` ` `// From Cell(1, 1)` ` ` `let d2 = R + C - 2;` ` ` `// From cell(N, 1)` ` ` `let d3 = N - R + C - 1;` ` ` `// From cell(1, M)` ` ` `let d4 = M - C + R - 1;` ` ` `// Finding out maximum` ` ` `let maxDistance = Math.max(d1, Math.max(` ` ` `d2, Math.max(d3, d4)));` ` ` `// Print the answer` ` ` `document.write(maxDistance);` `}` `// Driver Code` ` ` `let N = 15, M = 12, R = 1, C = 6;` ` ` ` ` `farthestCellDistance(N, M, R, C);` `// This code is contributed by Bobby` `</script>` |

**Output:**

20

**Time Complexity:** O(1), as we are not using any loops or recursion to traverse.**Auxiliary Space:** O(1), as we are not using any extra space.