#K206. Maximum Height Difference in a Grid
Maximum Height Difference in a Grid
Maximum Height Difference in a Grid
You are given a grid of size where each cell contains an integer representing its height. Your task is to find the maximum absolute difference between the heights of any two adjacent cells. Two cells are considered adjacent if they share a side (i.e., the cells directly above, below, left, or right of a given cell).
Formally, let denote the height of the cell in the -th row and -th column. You need to compute
[
\max_{(i,j) \sim (k,l)} \ |grid[i][j] - grid[k][l]|
]
where the maximum is taken over all pairs of adjacent cells and .
It is guaranteed that the grid will have at least one cell.
inputFormat
The first line of input contains two integers and , the number of rows and columns of the grid respectively. This is followed by lines, each containing space-separated integers representing the grid.
outputFormat
Output a single integer — the maximum absolute difference between the heights of any two adjacent cells.## sample
3 3
1 2 2
3 8 2
5 3 5
6
</p>