Introduction
In this problem, we are given a 2D grid and an integer x.
We can perform one operation where we either:
- Add
xto any grid element - Subtract
xfrom any grid element
Our goal is to make all elements in the grid equal using the minimum number of operations.
If making all values equal is not possible, we return -1.
This problem may initially look like a matrix manipulation question, but the actual logic is based on:
- Array transformation
- Median property
- Greedy optimization
# Problem Link
Problem Statement
You are given:
- A 2D integer grid of size
m × n - An integer
x
You can perform operations where you add or subtract x from any cell.
A grid becomes uni-value when all elements become equal.
Return the minimum number of operations needed.
If impossible, return -1.
Example
Example 1
Explanation:
We can make every element equal to 4.
- 2 → 4 (1 operation)
- 6 → 4 (1 operation)
- 8 → 4 (2 operations)
Total = 4 operations.
Key Observation
Before solving the problem, we must understand one important rule.
If we can add or subtract only x, then:
All numbers must belong to the same remainder group when divided by x.
Meaning:
Why?
Because if two numbers have different remainders, they can never become equal using only +x or -x operations.
Intuition
We need to convert all numbers into a single target value.
But what target value gives minimum operations?
The answer is:
Median
For minimizing total absolute distance, median gives the optimal answer.
Since every operation changes value by x, we can:
- Flatten grid into a list
- Sort the list
- Pick median as target
- Calculate operations required
Why Median Works?
Median minimizes:
For example:
If target = 2
If target = 5
Median gives minimum total distance.
Approach 1: Brute Force
We can try every possible number as target.
For each target:
- Calculate operations required
- Store minimum answer
Time Complexity
Where N = total grid elements
This is slow for large constraints.
Approach 2: Optimal Median Approach
This is the best approach.
Steps
Step 1: Flatten Grid
Convert 2D grid into 1D array.
Step 2: Sort Array
Sorting helps us find median.
Step 3: Check Validity
All values must have same remainder when divided by x.
Otherwise return -1.
Step 4: Pick Median
Median minimizes operations.
Step 5: Count Operations
For every element:
Java Solution
Code Explanation
Step 1: Flatten Matrix
We convert grid into list.
Step 2: Sort List
Sorting allows median selection.
Step 3: Check Possibility
If remainders differ, answer becomes impossible.
Step 4: Select Median
Median becomes target value.
Step 5: Calculate Operations
Difference divided by x gives operations.
Dry Run
Input:
Flatten:
Sort:
Median:
Operations:
Total:
Time Complexity
Flatten Grid
Sorting
Traverse Array
Total Complexity
Where:
Space Complexity
We store all elements in list.
Common Mistakes
1. Forgetting Mod Check
Many people directly calculate operations.
But without checking:
answer may become invalid.
2. Choosing Average Instead of Median
Average does not minimize absolute distance.
Median is required.
3. Not Sorting Before Finding Median
Median requires sorted array.
4. Forgetting Division by x
Operations are not direct difference.
Correct formula:
Edge Cases
Case 1
All values already equal.
Case 2
Different modulo values.
Case 3
Single cell grid.
FAQs
Q1. Why do we use median?
Median minimizes total absolute difference.
Q2. Why not average?
Average minimizes squared distance, not absolute operations.
Q3. Why modulo condition is important?
Because we can only move by multiples of x.
Q4. Can we solve without sorting?
Sorting is easiest way to get median.
Alternative median-finding algorithms exist but are unnecessary here.
Interview Insight
Interviewers ask this problem to test:
- Greedy thinking
- Median property
- Mathematical observation
- Array flattening
- Optimization logic
Conclusion
LeetCode 2033 is a great problem that combines math with greedy logic.
The most important learning points are:
- Flatten the grid
- Validate modulo condition
- Use median as target
- Calculate operations using difference divided by x
This approach is optimal and easy to implement.
Once you understand why median works, this problem becomes very straightforward.




