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++
#include <bits/stdc++.h>
using namespace std;
void farthestCellDistance( int N, int M,
int R, int C)
{
int d1 = N + M - R - C;
int d2 = R + C - 2;
int d3 = N - R + C - 1;
int d4 = M - C + R - 1;
int maxDistance = max(d1,
max(d2,
max(d3, d4)));
cout << maxDistance;
}
int main()
{
int N = 15, M = 12, R = 1, C = 6;
farthestCellDistance(N, M, R, C);
return 0;
}
|
Java
import java.io.*;
class GFG{
static void farthestCellDistance( int N, int M,
int R, int C)
{
int d1 = N + M - R - C;
int d2 = R + C - 2 ;
int d3 = N - R + C - 1 ;
int d4 = M - C + R - 1 ;
int maxDistance = Math.max(d1, Math.max(
d2, Math.max(d3, d4)));
System.out.println(maxDistance);
}
public static void main(String[] args)
{
int N = 15 , M = 12 , R = 1 , C = 6 ;
farthestCellDistance(N, M, R, C);
}
}
|
Python3
def farthestCellDistance(N, M, R, C):
d1 = N + M - R - C;
d2 = R + C - 2 ;
d3 = N - R + C - 1 ;
d4 = M - C + R - 1 ;
maxDistance = max (d1, max (d2, max (d3, d4)));
print (maxDistance);
if __name__ = = '__main__' :
N = 15 ;
M = 12 ;
R = 1 ;
C = 6 ;
farthestCellDistance(N, M, R, C);
|
C#
using System;
class GFG{
static void farthestCellDistance( int N, int M,
int R, int C)
{
int d1 = N + M - R - C;
int d2 = R + C - 2;
int d3 = N - R + C - 1;
int d4 = M - C + R - 1;
int maxDistance = Math.Max(d1, Math.Max(
d2, Math.Max(d3, d4)));
Console.WriteLine(maxDistance);
}
static public void Main()
{
int N = 15, M = 12, R = 1, C = 6;
farthestCellDistance(N, M, R, C);
}
}
|
Javascript
<script>
function farthestCellDistance( N, M,
R, C)
{
let d1 = N + M - R - C;
let d2 = R + C - 2;
let d3 = N - R + C - 1;
let d4 = M - C + R - 1;
let maxDistance = Math.max(d1, Math.max(
d2, Math.max(d3, d4)));
document.write(maxDistance);
}
let N = 15, M = 12, R = 1, C = 6;
farthestCellDistance(N, M, R, C);
</script>
|
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.