Difference Between Ones and Zeros in Row and Column - Problem

You are given a binary matrix grid of size m × n, where each cell contains either 0 or 1.

Your task is to create a difference matrix where each cell diff[i][j] represents the "balance" of ones versus zeros for position (i,j).

The formula:
diff[i][j] = onesInRow[i] + onesInCol[j] - zerosInRow[i] - zerosInCol[j]

In other words, for each position, you:

  • Count all ones in the same row and column
  • Count all zeros in the same row and column
  • Calculate: (ones count) - (zeros count)

Goal: Return the complete difference matrix.

Input & Output

example_1.py — Basic 3×3 Matrix
$ Input: grid = [[0,1,1],[1,0,1],[0,0,1]]
Output: [[0,0,4],[0,0,4],[-2,-2,2]]
💡 Note: For position (0,0): row 0 has 2 ones and 1 zero, column 0 has 1 one and 2 zeros. Formula: 2+1-1-2 = 0. For position (2,2): row 2 has 1 one and 2 zeros, column 2 has 3 ones and 0 zeros. Formula: 1+3-2-0 = 2.
example_2.py — All Ones Matrix
$ Input: grid = [[1,1],[1,1]]
Output: [[4,4],[4,4]]
💡 Note: Every row has 2 ones and 0 zeros, every column has 2 ones and 0 zeros. For any position: 2+2-0-0 = 4.
example_3.py — Single Cell Matrix
$ Input: grid = [[1]]
Output: [[2]]
💡 Note: The single row has 1 one and 0 zeros, the single column has 1 one and 0 zeros. Formula: 1+1-0-0 = 2.

Visualization

Tap to expand
🍽️ Restaurant Chain Rating System😞😊😊😊🎯😊😞😞😊1st St2nd St3rd StA AveB AveC Ave🔍 Analysis for Target Restaurant📍 Location: 2nd St & B Ave (currently 😞)🏪 2nd Street: 😊😞😊 → 2 positive, 1 negative🛣️ B Avenue: 😊😞😞 → 1 positive, 2 negative📊 Score: (2+1) - (1+2) = 0💡 Key Insight: Pre-count each street/avenue once!⚡ Optimization StrategyStep 1: Send scouts to count reviews on each streetStep 2: Send scouts to count reviews on each avenueStep 3: For each restaurant, instant lookup + formula🎯 Result: O(m×n) time instead of O(m×n×(m+n))
Understanding the Visualization
1
City Grid Setup
Each restaurant location has either positive (1) or negative (0) reviews
2
Street & Avenue Analysis
For each location, we consider all reviews on its street (row) and avenue (column)
3
Smart Preprocessing
Instead of walking the same streets repeatedly, send scouts once to count all reviews
4
Efficient Calculation
For each restaurant, quickly look up the pre-counted totals and apply the formula
Key Takeaway
🎯 Key Insight: By preprocessing row and column statistics once, we transform an O(m×n×(m+n)) brute force solution into an optimal O(m×n) approach, similar to how sending scouts once to count street reviews is far more efficient than walking the same streets repeatedly for each restaurant.

Time & Space Complexity

Time Complexity
⏱️
O(m × n)

Two passes: O(m×n) to compute statistics + O(m×n) to build result

n
2n
Linear Growth
Space Complexity
O(m + n)

Extra arrays to store ones count for each row and column

n
2n
Linearithmic Space

Constraints

  • m == grid.length
  • n == grid[i].length
  • 1 ≤ m, n ≤ 105
  • 1 ≤ m * n ≤ 105
  • grid[i][j] is either 0 or 1
Asked in
Google 32 Amazon 28 Microsoft 24 Meta 18
23.4K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen