Open In App

# 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 = 6
Output: 20
Explanation: 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 = 4
Output: 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 using namespace std; // Function to find the farthest// cell distance from the given cellvoid 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 Codeint main(){    int N = 15, M = 12, R = 1, C = 6;    farthestCellDistance(N, M, R, C);     return 0;}

## Java

 // Java program for the above approachimport java.io.*; class GFG{ // Function to find the farthest// cell distance from the given cellstatic 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 Codepublic 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 celldef 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 Codeif __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 approachusing System; class GFG{ // Function to find the farthest// cell distance from the given cellstatic 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 Codestatic 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



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.