-
Notifications
You must be signed in to change notification settings - Fork 0
/
Difference_Between.java
52 lines (48 loc) · 1.53 KB
/
Difference_Between.java
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
//2482. Difference Between Ones and Zeros in Row and Column
public class Difference_Between {
public static void main(String[] args) {
int[][] grid = { { 0, 1, 1 }, { 1, 0, 1 }, { 0, 0, 1 } };
onesMinusZeros(grid);
}
public static void onesMinusZeros(int[][] grid) {
int m = grid.length;
int n = grid[0].length;
int[][] diff = new int[m][n];
for (int i = 0; i < m; i++) {
int onerow = 0;
int onecolm = 0;
int zerorow = 0;
int zerocolm = 0;
int a = 0;
int b = 0;
for (int k = 0; k < m; k++) {
if (grid[k][i] == 0) {
zerocolm++;
} else {
zerorow ++;
}
b = zerocolm + zerorow;
}
for (int k = 0; k < n; k++) {
if (grid[i][k] == 0) {
onerow++;
} else {
onecolm++;
}
a = onerow + onecolm;
}
int x = a - b;
for (int j = 0; j < n; j++) {
if(onerow + onecolm == zerocolm + zerorow){
diff[i][j] = 0;
}
diff[i][j] = x;
}
}
for(int i = 0;i<m;i++){
for(int j = 0;j<n;j++){
System.out.println(diff[i][j]);
}
}
}
}