#C9101. Equalize Grid Operations

    ID: 53158 Type: Default 1000ms 256MiB

Equalize Grid Operations

Equalize Grid Operations

You are given a square grid of integers. In one operation, you can consider the entire grid and your goal is to equalize the grid using the following formula:

\[ \text{operations} = M - m + 1 \]

where \(M\) is the maximum element in the grid and \(m\) is the minimum element.

Your task is to compute the minimum number of operations required to make the grid equal based on the formula above.

Example:
For the grid 1 2 3 4 we have \(M = 4,\ m = 1\) and the answer is \(4 - 1 + 1 = 4\).

inputFormat

The input is given via standard input (stdin) and starts with an integer \(T\) representing the number of test cases. For each test case, the following input is provided:

  • An integer \(n\) representing the dimension of the square grid.
  • Next, \(n\) lines follow, each containing \(n\) space-separated integers representing the grid rows.

For example:

3
2
1 2
3 4
3
1 0 1
2 1 2
0 0 0
2
2 2
2 2

outputFormat

For each test case, output the minimum number of operations required to equalize the grid on a new line.

The output is printed to standard output (stdout).

For the sample input above, the expected output is:

4
3
1
## sample
1
2
1 2
3 4
4

</p>