Skip to content
This repository was archived by the owner on Apr 15, 2024. It is now read-only.

Commit 8a74b1f

Browse files
authored
Create 8-Efficient-PD.java
1 parent 14984b6 commit 8a74b1f

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

Array And Strings/8-Efficient-PD.java

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
public static void zeroMatrix(int[][] a) {
2+
3+
// only for one zero efficient O(n^2)
4+
// for more than one zero in a matrix O(n^3)
5+
6+
int x = -1, y = -1;
7+
8+
for(int i=0;i<a.length;i++) {
9+
for(int j=0;j<a[0].length;j++) {
10+
11+
if(a[i][j] == 0) {
12+
x = i;
13+
y = j;
14+
break;
15+
}
16+
17+
}
18+
}
19+
20+
if(x == -1 || y == -1)
21+
return;
22+
23+
for(int j=0;j<a[0].length;j++)
24+
a[x][j] = 0;
25+
26+
for(int i=0;i<a.length;i++)
27+
a[i][y] = 0;
28+
29+
for (int i=0;i<a.length;i++) {
30+
for (int j=0;j<a[0].length;j++)
31+
System.out.print(" " + a[i][j]);
32+
33+
System.out.println();
34+
}
35+
System.out.println();
36+
37+
}

0 commit comments

Comments
 (0)