Find the Good Matrix Problem in JavaGiven a 2-dimensional array ARR with N rows and M columns, where each element contains a value of 0 or 1, convert the given matrix into a Good matrix. In a Good matrix, if an element is 0, all elements in its row and column should also be 0. For example, if arr is the following matrix: Input Format: The first line of the input contains an integer, T, denoting the number of test cases. For each test case, the first line 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 updated matrix after updating the given matrix as described in the problem statement. Algorithm
Implementation:Filename: GoodMatrix.java Output: 0 1 1 0 0 0 0 0 0 1 1 0 0 0 0 0 Complexity Analysis: Time Complexity: The code iterates over each element in the given matrix, resulting in nested loops. The outer loop iterates 'row' from 0 to 'n - 1', where 'n' is the number of rows. Where'm' is the number of columns, the inner loop iterates 'col' from 0 to'm - 1'. Within the nested loops, there are additional loops for setting the row and column values to 0. As a result, the time complexity is denoted by the notation O(n * m * (n + m)), where n denotes the number of rows and m the number of columns. Space Complexity: The code creates a new 2-dimensional array 'answer' to store the modified matrix. The space required for the 'answer' array is the same as the input matrix: ' n' rows and 'm' columns. Therefore, the space complexity is O(n * m). Next TopicHow Streams Work in Java |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India