Watch 10 video solutions for Minimum Operations to Make a Uni-Value Grid, a medium level problem involving Array, Math, Sorting. This walkthrough by NeetCodeIO has 8,211 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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.
Example 1:
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:
Input: grid = [[1,5],[2,3]], x = 1 Output: 5 Explanation: We can make every element equal to 3.
Example 3:
Input: grid = [[1,2],[3,4]], x = 2 Output: -1 Explanation: It is impossible to make every element equal.
Constraints:
m == grid.lengthn == grid[i].length1 <= m, n <= 1051 <= m * n <= 1051 <= x, grid[i][j] <= 104Problem Overview: You are given an m x n grid and an integer x. In one operation, you can add or subtract x from any cell. The goal is to make every value in the grid equal using the minimum number of operations, or return -1 if it cannot be done.
The key observation: since each operation changes a value by exactly x, all numbers must belong to the same remainder group modulo x. If any element differs in value % x, reaching a single common value is impossible.
Approach 1: Heuristic with Median (Flatten + Sorting) (Time: O(mn log(mn)), Space: O(mn))
Flatten the matrix into a 1D array and check that every element has the same remainder when divided by x. If not, return -1 immediately. Otherwise, sort the array and pick the median value as the target.
The median minimizes the sum of absolute differences. For each element v, the number of operations required is |v - median| / x. Add these across all elements to compute the total operations. Sorting ensures you can find the median efficiently, and the absolute distance conversion turns the grid problem into a classic sorting and distance minimization task.
This works because converting all values to the median minimizes the total movement across the array. The approach is simple, deterministic, and performs well for typical grid sizes.
Approach 2: Dynamic Programming with Modulo Analysis (Time: O(mn * k), Space: O(k))
This method focuses on grouping values by the number of steps away from a base remainder class. After confirming that all elements share the same remainder modulo x, convert each value into a normalized step count: (value - base) / x. Now the problem becomes minimizing total moves to convert all step counts into one value.
Instead of sorting, you can build frequency counts of these normalized values and compute the minimum cost using prefix sums or dynamic programming over possible targets. Each transition evaluates how many increments or decrements are required to move elements toward a candidate value.
This technique emphasizes the mathematical structure of the problem and uses properties of arrays and math. While more complex than the median approach, it avoids explicitly sorting the entire dataset and can be useful when working with bounded ranges or precomputed frequencies.
Recommended for interviews: The median-based solution is what interviewers typically expect. It demonstrates that you recognize the modulo feasibility constraint and the median property for minimizing absolute distance. Mentioning the modulo check first shows strong problem insight. Implementing the flattened array + median approach quickly communicates practical algorithm skills.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Heuristic with Median (Flatten + Sort) | O(mn log(mn)) | O(mn) | General case; simplest and most common interview solution |
| Dynamic Programming with Modulo Analysis | O(mn * k) | O(k) | Useful when value ranges are bounded or frequency-based optimization is preferred |