in Education by
Want to improve this post? Provide detailed answers to this question, including citations and an explanation of why your answer is correct. Answers without enough detail may be edited or deleted. Given a NxN matrix with 0s and 1s. Set every row that contains a 0 to all 0s and set every column that contains a 0 to all 0s. For example 1 0 1 1 0 0 1 1 1 0 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 results in 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 1 1 0 A Microsoft Engineer told me that there is a solution that involves no extra memory, just two boolean variables and one pass, so I'm looking for that answer. BTW, imagine it is a bit matrix, therefore just 1s and 0s are allow to be in the matrix. JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
Ok, so I'm tired as it's 3AM here, but I have a first try inplace with exactly 2 passes on each number in the matrix, so in O(NxN) and it is linear in the size of the matrix. I use 1rst column and first row as markers to know where are rows/cols with only 1's. Then, there are 2 variables l and c to remember if 1rst row/column are all 1's also. So the first pass sets the markers and resets the rest to 0's. The second pass sets 1 in places where rows and cols where marked to be 1, and resets 1st line/col depending on l and c. I doubt strongly that I can be done in 1 pass as squares in the beginning depend on squares in the end. Maybe my 2nd pass can be made more efficient... import pprint m = [[1, 0, 1, 1, 0], [0, 1, 1, 1, 0], [1, 1, 1, 1, 1], [1, 0, 1, 1, 1], [1, 1, 1, 1, 1]] N = len(m) ### pass 1 # 1 rst line/column c = 1 for i in range(N): c &= m[i][0] l = 1 for i in range(1,N): l &= m[0][i] # other line/cols # use line1, col1 to keep only those with 1 for i in range(1,N): for j in range(1,N): if m[i][j] == 0: m[0][j] = 0 m[i][0] = 0 else: m[i][j] = 0 ### pass 2 # if line1 and col1 are ones: it is 1 for i in range(1,N): for j in range(1,N): if m[i][0] & m[0][j]: m[i][j] = 1 # 1rst row and col: reset if 0 if l == 0: for i in range(N): m [i][0] = 0 if c == 0: for j in range(1,N): m [0][j] = 0 pprint.pprint(m)

Related questions

0 votes
    Take from a pack of cards all the Aces, Kings, Queens and Jacks. Arrange them in a 4 × 4 square so that every ... and one card of each suit (Heart, Spade, Diamond, Club)....
asked Feb 11, 2021 in Education by JackTerrance
0 votes
    The intersection of a row and column is called cell. true or false Select the correct answer from above options...
asked Dec 31, 2021 in Education by JackTerrance
0 votes
    The intersection of a row and column is called cell. True or false Select the correct answer from above options...
asked Nov 29, 2021 in Education by JackTerrance
0 votes
    The intersection of a column and a row is called (a) Box (b) cell (c) Tool (d) square Select the correct answer from above options...
asked Dec 24, 2021 in Education by JackTerrance
0 votes
    ii) to find the largest value within the cell range starting from row 1 column A to row 25 column B [1+1] Select the correct answer from above options...
asked Dec 1, 2021 in Education by JackTerrance
0 votes
    The row formed at node a' in the cut set matrix in the figure shown below is? (a) +1 +1 +1 +1 ... theory Questions for GATE EC Exam, Network Theory MCQ (Multiple Choice Questions)...
asked Oct 20, 2021 in Education by JackTerrance
0 votes
    The row formed at node c' in the cut set matrix in the following figure? (a) -1 -1 0 0 +1 -1 ... theory Questions for GATE EC Exam, Network Theory MCQ (Multiple Choice Questions)...
asked Oct 20, 2021 in Education by JackTerrance
0 votes
    What is the function to set column names for a matrix? (a) names() (b) colnames() (c) col.names() ... Getting Started of R Programming Select the correct answer from above options...
asked Feb 15, 2022 in Education by JackTerrance
0 votes
    Which are indexed by either row or column using a specific name or number? (a) Datasets (b) Data frames (c ... and Out of R Programming Select the correct answer from above options...
asked Feb 13, 2022 in Education by JackTerrance
0 votes
    I have the dataframe as below: df ID val 1 0.0 2 yes 3 1.0 4 0.0 5 yes How do I match my ... use np.where along with the bfill? Select the correct answer from above options...
asked Jan 10, 2022 in Education by JackTerrance
0 votes
    A system is in a ______ state if there exists a set of transactions such that every transaction in the ... , Database Interview Questions and Answers for Freshers and Experience...
asked Oct 11, 2021 in Education by JackTerrance
0 votes
    The property (or set of properties) that uniquely defines each row in a table is called the: (a) ... Answers, Database Interview Questions and Answers for Freshers and Experience...
asked Oct 11, 2021 in Education by JackTerrance
0 votes
    I was trying to solve this hiring contest problem (now closed) Lexicographic Rows You are given a matrix of characters. In one ... { //Current is valid if (input[i - 1][j]...
asked Apr 16, 2022 in Education by JackTerrance
0 votes
    To bind a row onto an already existing matrix, the ______ function can be used. (a) Rbind (b) Sbnd (c) ... and Out of R Programming Select the correct answer from above options...
asked Feb 15, 2022 in Education by JackTerrance
...