Farthest cell from a given cell in a Matrix | Function to find the farthest cell distance from the given cell ; From cell ( N , M ) ; From Cell ( 1 , 1 ) ; From cell ( N , 1 ) ; From cell ( 1 , M ) ; Finding out maximum ; Print the answer ; Driver Code
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 ) ;