-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDanpheArray.java
More file actions
48 lines (42 loc) · 1.22 KB
/
Copy pathDanpheArray.java
File metadata and controls
48 lines (42 loc) · 1.22 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
public class DanpheArray {
public static void main(String[] args) {
System.out.println(isDanphe(new int[]{2, 4, 2}));
System.out.println(isDanphe(new int[]{1, 3, 17, -5}));
System.out.println(isDanphe(new int[]{2, 3, 5}));
System.out.println(isDanphe_another(new int[]{4, 8, 6, 3, 2, 9, 8, 11, 8, 13, 12, 12, 6}));
System.out.println(isDanphe_another(new int[]{2, 4, 6, 8, 6}));
System.out.println(isDanphe_another(new int[]{2, 8, 7, 10, -4, 6}));
}
static int isDanphe (int[] a) {
if(a.length == 0)
return 0;
boolean checkEven;
if (a[0] % 2 == 0) {
checkEven = true;
} else {
checkEven = false;
}
for(int i = 1; i < a.length; i++) {
if (checkEven) {
if (a[i] % 2 != 0)
return 0;
} else {
if (a[i] % 2 == 0)
return 0;
}
}
return 1;
}
static int isDanphe_another (int[] a) {
boolean oddFlag = false;
for(int i = 0, j = a.length - 1; i <= j; i++, j--) {
if((a[i] % 2 == 0 && a[j] % 2 != 0) || (a[i] % 2 != 0 && a[j] % 2 == 0))
return 0;
if(a[i] % 2 != 0 || a[j] % 2 != 0)
oddFlag = true;
}
if(oddFlag)
return 1;
return 0;
}
}