#C10844. Minimum Operations to Equalize Grid
Minimum Operations to Equalize Grid
Minimum Operations to Equalize Grid
You are given an \(N \times M\) grid where each cell contains an integer between 0 and 9. You are allowed to perform the following operation:
- Select any subrectangle of the grid and increase every element in that subrectangle by 1.
Your goal is to make all the grid elements equal using the minimum number of operations possible.
It can be shown that the minimum number of operations needed is equal to \(\max(A) - \min(A)\), where \(\max(A)\) and \(\min(A)\) denote the maximum and minimum values in the grid respectively.
Example: For a grid:
1 1 1 2 2 2 3 3 3
The answer is \(3-1=2\) operations.
inputFormat
The first line contains two integers \(N\) and \(M\) separated by a space, representing the number of rows and columns respectively.
This is followed by \(N\) lines, each containing \(M\) space-separated integers representing the grid values.
outputFormat
Output a single integer: the minimum number of operations required to equalize all the grid elements.
## sample3 3
1 1 1
2 2 2
3 3 3
2