28. Make Matrix Beautiful
β GFG solution to Make Matrix Beautiful problem: find minimum operations to make all row and column sums equal using greedy optimization approach. π
The problem can be found at the following link: π Question Link
π§© Problem Description
A beautiful matrix is defined as a square matrix in which the sum of elements in every row and every column is equal. Given a square matrix mat[][]
, your task is to determine the minimum number of operations required to make the matrix beautiful.
In one operation, you are allowed to increment the value of any single cell by 1.
π Examples
Example 1
Input: mat[][] = [[1, 2],
[3, 4]]
Output: 4
Explanation:
Increment value of cell(0, 0) by 3,
Increment value of cell(0, 1) by 1.
Matrix after the operations: [[4, 3],
[3, 4]]
Here, sum of each row and column is 7.
Hence total 4 operations are required.
Example 2
Input: mat[][] = [[1, 2, 3],
[4, 2, 3],
[3, 2, 1]]
Output: 6
Explanation:
Increment value of cell(0, 0) by 1,
Increment value of cell(0, 1) by 2,
Increment value of cell(2, 1) by 1,
Increment value of cell(2, 2) by 2.
Matrix after the operations: [[2, 4, 3],
[4, 2, 3],
[3, 3, 3]]
Here, sum of each row and column is 9.
Hence total 6 operations are required.
π Constraints
$1 \le \text{mat.size()} \le 900$
$0 \le \text{mat}[i][j] \le 10^6$
β
My Approach
The optimal approach uses a Greedy Strategy to find the maximum sum among all rows and columns, then calculate the minimum operations needed:
Greedy Maximum Sum Strategy
Find Maximum Sum:
Calculate the sum of each row and find the maximum.
Calculate the sum of each column and find the maximum.
The target sum for all rows and columns should be this maximum value.
Key Insight:
Since we can only increment values (never decrement), the target sum must be at least the maximum of all current row and column sums.
Any smaller target would be impossible to achieve for rows/columns that already exceed it.
Calculate Operations:
For each row, if its current sum is less than the target maximum, we need
(max - current_sum)
operations.Since we're only incrementing row elements, the total operations equals the sum of deficits for all rows.
Why This Works:
When we increment elements in rows to match the target, the column sums automatically increase.
The greedy choice of using the maximum possible sum ensures feasibility.
This approach guarantees the minimum number of operations.
π Time and Auxiliary Space Complexity
Expected Time Complexity: O(nΒ²), where n is the size of the matrix. We traverse the matrix three times: once for row sums, once for column sums, and once for calculating the result.
Expected Auxiliary Space Complexity: O(1), as we only use a constant amount of additional space for variables regardless of the matrix size.
π§ Code (C)
int balanceSum(int n, int mat[][n]) {
int max = 0, res = 0;
for (int i = 0; i < n; i++) {
int sum = 0;
for (int j = 0; j < n; j++) sum += mat[i][j];
if (sum > max) max = sum;
}
for (int j = 0; j < n; j++) {
int sum = 0;
for (int i = 0; i < n; i++) sum += mat[i][j];
if (sum > max) max = sum;
}
for (int i = 0; i < n; i++) {
int sum = 0;
for (int j = 0; j < n; j++) sum += mat[i][j];
res += max - sum;
}
return res;
}
π§βπ» Code (C++)
class Solution {
public:
int balanceSums(vector<vector<int>>& mat) {
int n = mat.size(), max = 0, res = 0;
for (int i = 0; i < n; i++) {
int sum = 0;
for (int j = 0; j < n; j++) sum += mat[i][j];
max = sum > max ? sum : max;
}
for (int j = 0; j < n; j++) {
int sum = 0;
for (int i = 0; i < n; i++) sum += mat[i][j];
max = sum > max ? sum : max;
}
for (int i = 0; i < n; i++) {
int sum = 0;
for (int j = 0; j < n; j++) sum += mat[i][j];
res += max - sum;
}
return res;
}
};
β Code (Java)
class Solution {
public static int balanceSums(int[][] mat) {
int n = mat.length, max = 0, res = 0;
for (int i = 0; i < n; i++) {
int sum = 0;
for (int j = 0; j < n; j++) sum += mat[i][j];
max = Math.max(sum, max);
}
for (int j = 0; j < n; j++) {
int sum = 0;
for (int i = 0; i < n; i++) sum += mat[i][j];
max = Math.max(sum, max);
}
for (int i = 0; i < n; i++) {
int sum = 0;
for (int j = 0; j < n; j++) sum += mat[i][j];
res += max - sum;
}
return res;
}
}
π Code (Python)
class Solution:
def balanceSums(self, mat):
n, max_sum, res = len(mat), 0, 0
for i in range(n):
sum_val = sum(mat[i])
max_sum = max(sum_val, max_sum)
for j in range(n):
sum_val = sum(mat[i][j] for i in range(n))
max_sum = max(sum_val, max_sum)
for i in range(n):
res += max_sum - sum(mat[i])
return res
π§ Contribution and Support
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: π¬ Any Questions?. Let's make this learning journey more collaborative!
β If you find this helpful, please give this repository a star! β
πVisitor Count
Last updated