-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrix_cell_increment.java
48 lines (47 loc) · 1.41 KB
/
matrix_cell_increment.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
public class matrix_cell_increment {
public static void main(String[] args) {
int m = 2, n = 2;
int[][] indices = {{1,1},{0,0}};
System.out.println(oddCells(m,n,indices));
}
public static int oddCells(int m, int n, int[][] indices) {
int[][] matrix = new int[m][n];
int odd = 0;
// for (int i = 0; i < matrix.length; i++) {
// for (int j = 0; j < matrix[i].length; j++) {
// matrix[i][j] = 0;
// }
// }
for (int i = 0; i < indices.length; i++) {
incrementCell(matrix, indices[i][0], indices[i][1]);
}
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
if( matrix[i][j]%2!=0)
{
odd++;
}
}
}
return odd;
}
static void incrementCell(int[][] matrix, int row, int col)
{
for (int i = 0; i < matrix[row].length; i++) {
// for (int j = 0; j < matrix[i].length; j++) {
// if(i==row)
// {
// matrix[i][j]++;
// }
// if(j==col)
// {
// matrix[i][j]++;
// }
// }
matrix[row][i]++;
}
for (int i = 0; i < matrix.length; i++) {
matrix[i][col]++;
}
}
}