Difference Between Ones and Zeros in Row and Column

As a Systems Engineer at Tata Consultancy Services, I deliver exceptional software products for mobile and web platforms, using agile methodologies and robust quality maintenance. I am experienced in performance testing, automation testing, API testing, and manual testing, with various tools and technologies such as Jmeter, Azure LoadTest, Selenium, Java, OOPS, Maven, TestNG, and Postman.
I have successfully developed and executed detailed test plans, test cases, and scripts for Android and web applications, ensuring high-quality standards and user satisfaction. I have also demonstrated my proficiency in manual REST API testing with Postman, as well as in end-to-end performance and automation testing using Jmeter and selenium with Java, TestNG and Maven. Additionally, I have utilized Azure DevOps for bug tracking and issue management.
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
i<sup>th</sup>row beonesRow<sub>i</sub>.Let the number of ones in the
j<sup>th</sup>column beonesCol<sub>j</sub>.Let the number of zeros in the
i<sup>th</sup>row bezerosRow<sub>i</sub>.Let the number of zeros in the
j<sup>th</sup>column bezerosCol<sub>j</sub>.diff[i][j] = onesRow<sub>i</sub> + onesCol<sub>j</sub> - zerosRow<sub>i</sub> - zerosCol<sub>j</sub>
Return the difference matrix diff.
LeetCode Problem - 2482
class Solution {
public int[][] onesMinusZeros(int[][] grid) {
int rows = grid.length; // Number of rows in the grid
int cols = grid[0].length; // Number of columns in the grid
int[][] diff = new int[rows][cols]; // Resulting difference grid
int[] rowDiff = new int[rows]; // Array to store the count of 1s in each row
int[] colDiff = new int[cols]; // Array to store the count of 1s in each column
// First pass: Count the number of 1s in each row and each column
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (grid[i][j] == 1) {
rowDiff[i]++; // Increment the count of 1s in row i
colDiff[j]++; // Increment the count of 1s in column j
}
}
}
// Second pass: Calculate the result for each cell in the diff array
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
// Calculate the number of 1s and 0s in the row and column of the current cell
int onesRowi = rowDiff[i]; // Number of 1s in the current row
int onesColj = colDiff[j]; // Number of 1s in the current column
int zerosRowi = cols - rowDiff[i]; // Number of 0s in the current row
int zerosColj = rows - colDiff[j]; // Number of 0s in the current column
// Calculate the difference as the number of 1s in the row and column minus the number of 0s
diff[i][j] = onesRowi + onesColj - zerosRowi - zerosColj;
}
}
// Return the resulting grid with differences
return diff;
}
}




