Reset Matrix
You have given a 2-dimensional array ‘ARR’ with ‘N’ rows and ‘M’ columns in which each element contains only two values,i.e., 0 and 1. Your task is to convert the given matrix into the Good matrix in which if an element is 0, you need to set all elements values present in its entire row and column to 0.
For example:
Consider ARR = [[1 , 0 , 1] ,
[1 , 1 , 1] ,
[1 , 1 , 1]],
the Good matrix after updating the given matrix as described in the question is
[[0 , 0 , 0] ,
[1 , 0 , 1] ,
[1 , 0 , 1]].
Since ARR[0][1] is 0, we need to set all element’s values present in 0-th row and 1-th column to 0.
Note :
You do not need to print the matrix. Just change in the given input.
Input format :
The first line of the input contains an integer, 'T,’ denoting the number of test cases.
The first line of each test case contains two integers, 'N' and ‘M’, denoting the number of rows and columns in the array.
The Next 'N' lines of each test case contain 'M' space-separated integers denoting the elements of the array 'ARR'.
Output format :
For each test case, return the Good matrix after updating the given matrix as described in the question.
Print the output of each test case in a separate line
Constraints :
1 <= T <= 20
1 <= N <= 300
1 <= M <= 300
ARR[i][j] can only contain two values, i.e, 0 and 1.
Where 'T' denotes the number of test cases, 'N' and 'M' denotes the number of rows and the number of columns in the array ‘ARR’ respectively, and 'ARR[i][j]' denotes the ’j-th’ element of 'i-th' row of the array 'ARR'.
Time Limit: 1sec
CodingNinjas
author
2y
Brute Force
A simple method is to create a 2-dimensional array to store the changes we need to update in the original matrix to convert the given matrix into the Good matrix.
Our approach will be to cr...read more
CodingNinjas
author
2y
Store rows and columns
The basic idea is to create an array for all rows and an array for all columns in which we will keep the information of those rows and columns whose value we need to set as 0 for...read more
CodingNinjas
author
2y
Optimized approach
As we have discussed in the previous approach, we have created an array for all rows and an array for all columns to keep information of those rows and columns whose value we need to...read more
Add answer anonymously...
Top Infosys System Engineer Specialist interview questions & answers
Top HR questions asked in Infosys System Engineer Specialist
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app