> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Minimum Operations to Make a Uni-Value Grid

> Tested Python solution for LeetCode 2033 with 18 pytest cases. Generate a practice environment with lcpy.

LeetCode 2033, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Math](/catalog/topics/math), [Sorting](/catalog/topics/sorting), [Matrix](/catalog/topics/matrix). [View on LeetCode](https://leetcode.com/problems/minimum-operations-to-make-a-uni-value-grid/description/).

Generate this problem as a practice environment: tested reference solution, 18 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 2033   # by problem number
lcpy gen -s minimum_operations_to_make_a_uni_value_grid   # by problem name
```

## Problem

You are given a 2D integer grid of size m x n and an integer x. In one operation, you can add x to or subtract x from any element in the grid.

A uni-value grid is a grid where all the elements of it are equal.

Return the minimum number of operations to make the grid uni-value. If it is not possible, return -1.

### Examples

![Example 1](https://assets.leetcode.com/uploads/2021/09/21/gridtxt.png)

```
Input: grid = [[2,4],[6,8]], x = 2
Output: 4
Explanation: We can make every element equal to 4 by doing the following:
- Add x to 2 once.
- Subtract x from 6 once.
- Subtract x from 8 twice.
A total of 4 operations were used.
```

![Example 2](https://assets.leetcode.com/uploads/2021/09/21/gridtxt-1.png)

```
Input: grid = [[1,5],[2,3]], x = 1
Output: 5
Explanation: We can make every element equal to 3.
```

![Example 3](https://assets.leetcode.com/uploads/2021/09/21/gridtxt-2.png)

```
Input: grid = [[1,2],[3,4]], x = 2
Output: -1
Explanation: It is impossible to make every element equal.
```

### Constraints

* m == grid.length
* n == grid\[i].length
* 1 \<= m, n \<= 10^5
* 1 \<= m \* n \<= 10^5
* 1 \<= x, grid\[i]\[j] \<= 10^4

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/minimum_operations_to_make_a_uni_value_grid/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/minimum_operations_to_make_a_uni_value_grid/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(m*n log(m*n))
    # Space: O(m*n)
    def min_operations(self, grid: list[list[int]], x: int) -> int:
        vals = [v for row in grid for v in row]
        rem = vals[0] % x
        if any(v % x != rem for v in vals):
            return -1
        vals.sort()
        median = vals[len(vals) // 2]
        return sum(abs(v - median) // x for v in vals)
```

## Complexity

| Time | Space |
| - | - |
| O(m*n log(m*n)) | O(m\*n) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.