2482. Difference Between Ones and Zeros in Row and Column LeetCode Solution
In this guide, you will get 2482. Difference Between Ones and Zeros in Row and Column LeetCode Solution with the best time and space complexity. The solution to Difference Between Ones and Zeros in Row and Column problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Difference Between Ones and Zeros in Row and Column solution in C++
- Difference Between Ones and Zeros in Row and Column solution in Java
- Difference Between Ones and Zeros in Row and Column solution in Python
- Additional Resources

Problem Statement of Difference Between Ones and Zeros in Row and Column
You are given a 0-indexed m x n binary matrix grid.
A 0-indexed m x n difference matrix diff is created with the following procedure:
Let the number of ones in the ith row be onesRowi.
Let the number of ones in the jth column be onesColj.
Let the number of zeros in the ith row be zerosRowi.
Let the number of zeros in the jth column be zerosColj.
diff[i][j] = onesRowi + onesColj – zerosRowi – zerosColj
Return the difference matrix diff.
Example 1:
Input: grid = [[0,1,1],[1,0,1],[0,0,1]]
Output: [[0,0,4],[0,0,4],[-2,-2,2]]
Explanation:
– diff[0][0] = onesRow0 + onesCol0 – zerosRow0 – zerosCol0 = 2 + 1 – 1 – 2 = 0
– diff[0][1] = onesRow0 + onesCol1 – zerosRow0 – zerosCol1 = 2 + 1 – 1 – 2 = 0
– diff[0][2] = onesRow0 + onesCol2 – zerosRow0 – zerosCol2 = 2 + 3 – 1 – 0 = 4
– diff[1][0] = onesRow1 + onesCol0 – zerosRow1 – zerosCol0 = 2 + 1 – 1 – 2 = 0
– diff[1][1] = onesRow1 + onesCol1 – zerosRow1 – zerosCol1 = 2 + 1 – 1 – 2 = 0
– diff[1][2] = onesRow1 + onesCol2 – zerosRow1 – zerosCol2 = 2 + 3 – 1 – 0 = 4
– diff[2][0] = onesRow2 + onesCol0 – zerosRow2 – zerosCol0 = 1 + 1 – 2 – 2 = -2
– diff[2][1] = onesRow2 + onesCol1 – zerosRow2 – zerosCol1 = 1 + 1 – 2 – 2 = -2
– diff[2][2] = onesRow2 + onesCol2 – zerosRow2 – zerosCol2 = 1 + 3 – 2 – 0 = 2
Example 2:
Input: grid = [[1,1,1],[1,1,1]]
Output: [[5,5,5],[5,5,5]]
Explanation:
– diff[0][0] = onesRow0 + onesCol0 – zerosRow0 – zerosCol0 = 3 + 2 – 0 – 0 = 5
– diff[0][1] = onesRow0 + onesCol1 – zerosRow0 – zerosCol1 = 3 + 2 – 0 – 0 = 5
– diff[0][2] = onesRow0 + onesCol2 – zerosRow0 – zerosCol2 = 3 + 2 – 0 – 0 = 5
– diff[1][0] = onesRow1 + onesCol0 – zerosRow1 – zerosCol0 = 3 + 2 – 0 – 0 = 5
– diff[1][1] = onesRow1 + onesCol1 – zerosRow1 – zerosCol1 = 3 + 2 – 0 – 0 = 5
– diff[1][2] = onesRow1 + onesCol2 – zerosRow1 – zerosCol2 = 3 + 2 – 0 – 0 = 5
Constraints:
m == grid.length
n == grid[i].length
1 <= m, n <= 105
1 <= m * n <= 105
grid[i][j] is either 0 or 1.
Complexity Analysis
- Time Complexity: O(mn)
- Space Complexity: O(mn)
2482. Difference Between Ones and Zeros in Row and Column LeetCode Solution in C++
class Solution {
public:
vector<vector<int>> onesMinusZeros(vector<vector<int>>& grid) {
const int m = grid.size();
const int n = grid[0].size();
vector<vector<int>> ans(m, vector<int>(n));
vector<int> onesRow;
vector<int> onesCol;
for (const vector<int>& row : grid)
onesRow.push_back(ranges::count(row, 1));
for (int j = 0; j < n; ++j) {
int ones = 0;
for (int i = 0; i < m; ++i)
ones += grid[i][j];
onesCol.push_back(ones);
}
for (int i = 0; i < m; ++i)
for (int j = 0; j < n; ++j)
ans[i][j] =
onesRow[i] + onesCol[j] - (n - onesRow[i]) - (m - onesCol[j]);
return ans;
}
};
/* code provided by PROGIEZ */
2482. Difference Between Ones and Zeros in Row and Column LeetCode Solution in Java
class Solution {
public int[][] onesMinusZeros(int[][] grid) {
final int m = grid.length;
final int n = grid[0].length;
int[][] ans = new int[m][n];
int[] onesRow = new int[m];
int[] onesCol = new int[n];
for (int i = 0; i < m; ++i)
onesRow[i] = (int) Arrays.stream(grid[i]).filter(a -> a == 1).count();
for (int j = 0; j < n; ++j) {
int ones = 0;
for (int i = 0; i < m; ++i)
ones += grid[i][j];
onesCol[j] = ones;
}
for (int i = 0; i < m; ++i)
for (int j = 0; j < n; ++j)
ans[i][j] = onesRow[i] + onesCol[j] - (n - onesRow[i]) - (m - onesCol[j]);
return ans;
}
}
// code provided by PROGIEZ
2482. Difference Between Ones and Zeros in Row and Column LeetCode Solution in Python
class Solution:
def onesMinusZeros(self, grid: list[list[int]]) -> list[list[int]]:
m = len(grid)
n = len(grid[0])
ans = [[0] * n for _ in range(m)]
onesRow = [row.count(1) for row in grid]
onesCol = [col.count(1) for col in zip(*grid)]
for i in range(m):
for j in range(n):
ans[i][j] = (onesRow[i] + onesCol[j] -
(n - onesRow[i]) - (m - onesCol[j]))
return ans
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.