-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrixZeroPadding.java
More file actions
63 lines (61 loc) · 1.08 KB
/
Copy pathMatrixZeroPadding.java
File metadata and controls
63 lines (61 loc) · 1.08 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
56
57
58
59
60
61
62
63
public class MatrixZeroPadding {
public static void main(String[] args)
{
int m=3;
int n=4;
//Preparing the input matrix
int[][] matrix=new int[m][n];
int value=1;
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
matrix[i][j]=value;
value++;
}
}
matrix[2][1]=0;
matrix[2][2]=0;
displayMatrix(matrix, m, n);
int[][] output=zeroPadding(matrix,m,n);
System.out.println("After Zero Padding");
displayMatrix(output, m, n);
}
public static int[][] zeroPadding(int[][] matrix,int rows,int columns)
{
int[][] flagForZero=new int[rows][columns];
for(int i=0;i<rows;i++)
{
for(int j=0;j<columns;j++)
{
if (matrix[i][j]==0)
{
for(int k=0;k<columns;k++)
flagForZero[i][k]=1;
for(int l=0;l<rows;l++)
flagForZero[l][j]=1;
}
}
}
for(int i=0;i<rows;i++)
{
for(int j=0;j<columns;j++)
{
if (flagForZero[i][j]==1)
matrix[i][j]=0;
}
}
return matrix;
}
public static void displayMatrix(int[][] matrix,int rows,int columns)
{
for(int i=0;i<rows;i++)
{
for(int j=0;j<columns;j++)
{
System.out.print(matrix[i][j]+ " ");
}
System.out.println();
}
}
}