You have been given a binary matrix ‘MAT’ of size ‘N’ * ’N’. Let ‘i’, ’j’ denote the row and column of the matrix, respectively. If ‘MAT’[i][j] is equal to 0, flip every 1 in the ‘i’th row and ‘j’th column i.e., in the same row and the column as 0.
Your task is to return the total number of flips done over all the elements of the matrix.
Note:
1. Each element in the matrix 'MAT' is either a ‘0’ or ‘1.’
2. The flip operation is performed only for the 0s in the original matrix, and not for the new 0s formed as a result of flipping 1s in the original matrix.
3. If a cell is already flipped, don’t flip it twice.
4. Return the minimum number of flips needed.
For example:
Let the matrix be:
Then we return 6. As shown in the figure, the cells marked in red will be counted as they lie in the same row or column as 0 and will be flipped.
Input format:
The first line of input contains ‘T’ denoting the number of test cases.
The first line of each test case contains ‘N’ denoting the dimensions of the ‘N * N’ matrix.
The next ‘N’ lines contain ‘N’ single space-separated integers denoting the matrix 'MAT’.
Output format:
For each test case, print a single line that contains an integer that denotes the total number of flips made.
Note:
You don't have to print the output it has been taken care of. Just implement the given function.
Constraints:
1 <= T <= 5
0 <= N <= 100
MAT[i][j] = 0 or 1
Where ‘T’ is the number of test cases and ‘N’ is the number of rows and columns of the matrix 'MAT'.
Time limit: 1 sec
I first applied brute force but the solution was exceeding the time limit so I looked for ways to avoid traversing every
row and column of the matrix. Take the input of the matrix and in a map stored t...read more
The main idea is to count the number of 1s in the same row or column as any of the 0 in the given matrix. To do that, we iterate through the matrix with the variable ‘i’ for the ro...read more
The main idea is to ensure that a particular row or column is already checked, we need not check it again. We can do this by maintaining a visited array for rows and columns and chec...read more
The main idea is to calculate the number of zeros in the matrix, the number of rows that have zero, and the number of columns that have zeros.
Then the final answer = (ROWS * N) + ((...read more
Top Deutsche Bank Software Developer Intern interview questions & answers
Popular interview questions of Software Developer Intern
Top HR questions asked in Deutsche Bank Software Developer Intern
Reviews
Interviews
Salaries
Users/Month