Matrix Flip Bit

Easy
0/40
Average time to solve is 10m
profile
Contributed by
24 upvotes
Asked in companies
Deutsche BankQualcomm

Problem statement

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:

insert eg 1

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.

insert eg 2

Detailed explanation ( Input/output format, Notes, Images )
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
Sample Input 1:
1
4
1 0 1 1
1 1 0 1
1 1 1 1
1 1 0 1
Sample Output 1:
11
Explanation of sample input 1:
Test case 1:

inserteg-1

We can see that ‘MAT’[0][1] is zero so we flip all elements which are 1 in the ith row i.e 0 th row and jth column i.e. 1st column. We can clearly see that there are 3 1s in the 0th row and 3 1s in the 1st column. Hence for the ‘0’ at ‘MAT’[0][1] we make 3 + 3 = 6 flips.

We mark the already counted cells red so we don’t count them twice.

insert eg-2

Now we encounter the next zero at ‘MAT’[2][1]. We flip the bits in the first column which are unflipped and are 1 i.e ‘MAT’[2][2] and also the 1s which are unflipped in the second row i.e ‘MAT’[1][0] and ‘MAT’[1][3].

Finally, the count is 6 + 3 = 9, as shown with the cells marked in red.

insert eg-3

Now the last cell which is ‘0’ i.e ‘MAT’[3][2]. We check for all the cells in the third row and second column and mark all the 1s which are not marked red and increment the count.

Finally, we will have the following matrix.

insert eg 4

We see that there are 11 cells marked red which indicate that we flipped 11 cells so we return 11.
Sample Input 2:
2
5
1 1 0 0 1
1 0 1 1 0
1 1 1 1 1
0 0 1 1 0
1 0 1 0 1
6
0 1 1 1 1 1 
1 1 0 0 0 1 
0 0 1 0 1 1 
0 1 0 0 1 0 
1 0 1 0 1 1 
1 0 0 0 1 1 
Sample Output 2:
16
20 
Hint

Brute force all  0s and find the number of bits to be flipped

Approaches (3)
Brute Force Approach

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 row and ‘j’ for the column, and if for any cell ‘MAT’[i][j] = 0, we iterate through the ‘i’th row and ‘j’th column and count the number of 1s in them and mark them as -1 so that we do not revisit them. Finally, we return the ‘COUNT’.

 

  • Take a variable ‘COUNT’ to count the number of bits to be flipped.
  • Iterate through the matrix with the variable ‘i’ for row and variable ‘j’ for column and check for each matrix element if ‘MAT’[i][j] is zero.
    • If ‘MAT’[i][j] is zero, we iterate in the ‘i'th row and ‘j’th column using a variable ‘k’ and increment the count if ‘MAT’[i][k] or ‘MAT’[k][j] is 1.
    • If the above conditions are satisfied, we also mark the cell as -1 to not count it again.
  • Finally, we return the ‘COUNT’.
Time Complexity

O(N ^ 3),  where ‘N’ is the number of rows and columns of the matrix ‘MAT’.

 

In the worst case, we can have all 0s and in that case, each element will have an order of ‘N’ loop so the overall complexity will be the order of O(N ^ 3).

Space Complexity

O(1).

 

Since we are not using any extra space to store the elements.

Code Solution
(100% EXP penalty)
Matrix Flip Bit
Full screen
Console