-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhungryrabbit.java
More file actions
72 lines (71 loc) · 1.31 KB
/
hungryrabbit.java
File metadata and controls
72 lines (71 loc) · 1.31 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
64
65
66
67
68
69
70
71
72
public class solution {
public static int[] findCenter(int[][] mat){
int[] center = new int[2];
int rowSize = mat.length;
int colSize = mat[0].length;
if (isEven(rowSize) && isEven(colSize)){
int row1 = rowSize/2 -1;
int row2 = rowSize/2;
int col1 = colSize/2 -1;
int col2 = colSize/2;
int topleft = mat[row1][col1];
int topright = mat[row1][col2];
int botleft = mat[row2][col1];
int botright = mat[row2][col2];
int max = topleft;
center[0] = row1;
center[1] = col1;
if (topright > max){
max = topright;
center[0] = row1;
center[1] = col2;
}
if(botleft > max){
max = botleft;
center[0] = row2;
center[1] = col1;
}
if(botright > max){
max = botright;
center[0] = row2;
center[1] = col2;
}
}
else if(isEven(rowSize) && !isEven(colSize)){
int col = colSize/2;
center[1] = col;
int row1 = rowSize/2 -1;
int row2 = rowSize/2;
if(mat[row1][col] > mat[row2][col]){
center[0] = row1;
}
else{
center[0] = row2;
}
}
else if(!isEven(rowSize) && isEven(colSize)){
int row = rowSize/2;
center[0] = row;
int col1 = colSize/2 -1;
int col2 = colSize/2;
if (mat[row][col1] > mat[row][col2]){
center[1] = col1;
}
else{
center[1] = col2;
}
}
// !isEven(rowSize) && !isEven(colSize)
else{
center[0] = rowSize/2;
center[1] = colSize/2;
}
return center;
}
public static boolean isEven(int n){
if (n % 2 == 0) {
return true;
} else {
return false;
}
}