-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmakerowcolzero
More file actions
55 lines (52 loc) · 2 KB
/
makerowcolzero
File metadata and controls
55 lines (52 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/* Programmer : Dhruv Patel
* Problem Name : makerowcolzero
* Used In : Practice
* Used As : Matrix
* Problem :
* You are given the MxN matrix with numbers.Once you see the '0' you have to make whole row and column
* referring cell to the zero.
* Example :
* Input :-
* { {1,2,3,4,0,9},
* {8,9,10,11,12,3}}
* Output :-
* {{0,0,0,0,0,0},
* {8,9,10,11,0,13}}
* Thoughts =>
* Brute Force / Naive Approach :- We identify the row X column position and with respect to that
* which triggers two separate loops to make the values zero.
*/
class Solution {
public static int[][] makerowcolzero(int matrix[][], int row, int column) {
for (int k = 0; k < matrix[row].length; k++) {
matrix[row][k] = 0;
}
for (int c = 0; c < matrix[c].length && column < matrix[c].length; c++) { // condition for MxN matrix to handle Nth column.
matrix[c][column] = 0;
}
return matrix;
}
public static void print(int matrix[][]) { // Matrix printing utility.
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
System.out.print(matrix[i][j]);
}
System.out.println(" ");
}
}
public static void main(String args[]) {
int matrix[][] = {{1, 2, 3, 4, 5},
{0}
};
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
if (matrix[i][j] == 0) {
makerowcolzero(matrix, i, j);
print(matrix);
System.out.println("Broke bro");
break;
}
}
}
}
}