Ctrl K

Cross Balance of Ones

Medium
You get a rectangular m × n table grid of zeros and ones. For every cell (i, j) look at its "cross": row i together with column j. Compute: - the number of ones in row i plus the number of ones in column j, - minus the number of zeros in row i and minus the number of zeros in column j. Return a table of the same size where cell (i, j) holds that value.
Example 1
grid=[[1, 0, 1], [0, 0, 1]] → [[1, -1, 3], [-1, -3, 1]]

For cell (0, 0): row 0 has two ones and one zero, column 0 has one one and one zero: 2 + 1 - 1 - 1 = 1. For cell (1, 2): row 1 has one one and two zeros, column 2 has two ones: 1 + 2 - 2 - 0 = 1.

Example 2
grid=[[0, 0], [0, 0]] → [[-4, -4], [-4, -4]]

There are no ones at all: every row and column has two zeros, 0 + 0 - 2 - 2 = -4.

Example 3
grid=[[1], [1], [0]] → [[2], [2], [0]]
Constraints
  • 1 ≤ m, n
  • m · n ≤ 10⁵
  • all rows have the same length
  • grid[i][j] is 0 or 1
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsSpiral Readout MediumSpiral-Numbered Square MediumNext Step of a Cell Colony MediumZigzag Diagonal Sweep MediumMatch-Three Cascade Medium
loading Python…
Input
grid=[[1, 0, 1], [0, 0, 1]]
Expected
[[1, -1, 3], [-1, -3, 1]]

Run checks the first 3 tests in your browser. Submit checks all 70 tests on the server, hidden ones included.