-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOnionArray.java
More file actions
44 lines (38 loc) · 1.07 KB
/
Copy pathOnionArray.java
File metadata and controls
44 lines (38 loc) · 1.07 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
/**
*Created on 6/23/18
*/
public class OnionArray {
public static void main (String[] args) {
System.out.println(isOnionArray(new int[]{1, 2, 19, 4, 5}));
System.out.println(isOnionArray(new int[]{1, 2, 3, 4, 15}));
System.out.println(isOnionArray(new int[]{1, 3, 9, 8}));
System.out.println(isOnionArray(new int[]{2}));
System.out.println(isOnionArray(new int[]{}));
System.out.println(isOnionArray(new int[]{-2, 5, 0, 5, 12}));
}
static int isOnionArray(int[] a) {
if (a.length == 0) return 1;
else if(a.length == 1) {
if(a[0] <= 10) return 1;
else return 0;
}
for (int i = 0, j = a.length - 1; i < j; i++, j--) {
//if (i + j == a.length) {
if (a[i] + a[j] > 10)
return 0;
//}
}
return 1;
}
static int isOnionArray1(int[] a) {
if (a.length == 1 || a.length == 0)
return 1;
for (int i = 1, j = a.length - 1; i < j; i++, j--) {
if (i + j == a.length) {
if (a[i] + a[j] > 10)
return 0;
}
}
return 1;
}
}