-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHollowArray.java
More file actions
119 lines (100 loc) · 3.15 KB
/
Copy pathHollowArray.java
File metadata and controls
119 lines (100 loc) · 3.15 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
public class HollowArray {
public static void main(String[] args) {
System.out.println(isHollow(new int[]{1, 2, 4, 0, 0, 0, 3, 4, 5}));
System.out.println(isHollow(new int[]{1, 2, 0, 0, 0, 3, 4, 5}));
System.out.println(isHollow(new int[]{1, 2, 4, 9, 0, 0, 0, 3, 4, 5}));
System.out.println(isHollow(new int[]{1, 2, 0, 0, 3, 4}));
System.out.println(isHollow(new int[]{1, 2, 0, 0, 0, 3, 4}));
System.out.println(isHollow(new int[]{1, 1, 1, 1, 0, 0, 0, 0, 0, 2, 1, 2, 18}));
System.out.println(isHollow(new int[]{1, 2, 0, 0, 3, 4}));
System.out.println(isHollow(new int[]{1, 2, 0, 0, 0, 3, 4, 5}));
System.out.println(isHollow(new int[]{3, 8, 3, 0, 0, 0, 3, 3}));
System.out.println(isHollow(new int[]{1, 2, 0, 0, 0, 3, 4, 0}));
System.out.println(isHollow(new int[]{0, 1, 2, 0, 0, 0, 3, 4}));
System.out.println(isHollow(new int[]{0, 0, 0}));
System.out.println(isHollow(new int[]{0, 0, 1, 0, 0}));
System.out.println(isHollow(new int[]{0, 0, 1, 0, 1, 0, 0}));
}
static int isHollow (int[] a) {
if(a.length < 3)
return 0;
int len = a.length;
if(a[len/2] != 0 || a[len/2 + 1] != 0 || a[len/2 - 1] != 0)
return 0;
for(int i = 0, j = len - 1; i < j; i++, j--) {
if((a[i] != 0 && a[j] == 0) || (a[i] == 0 && a[j] != 0))
return 0;
}
return 1;
}
static int isHollow3 (int[] a) {
if (a.length < 3)
return 0;
int nonZerosLeft = 0, nonZerosRight = 0, countZero = 0;
int i, j;
for (i = 0; i < a.length; i++) {
if (a[i] != 0)
nonZerosLeft++;
else
break;
}
for (j = a.length - 1; j >= 0; j--) {
if (a[j] != 0)
nonZerosRight++;
else
break;
}
if ((nonZerosLeft != nonZerosRight)) // can add this additional condition here (j - i < 2)
return 0;
for (int k = i; k <= j; k++) {
if (a[k] == 0)
countZero++;
else return 0;
}
if (countZero >= 3)
return 1;
return 0;
}
static int isHollow1 (int[] a) {
if (a.length < 3)
return 0;
int countZero = 0, countNonZero = 0;
for (int i = 0, j = a.length - 1; i <= j; i++, j--) {
if (i == j && a[i] == 0) {
countZero++;
break;
}
if (a[i] != 0 && a[j] != 0 && countZero == 0)
countNonZero++;
else if (a[i] == 0 && a[j] == 0)
countZero = countZero + 2;
else
return 0;
}
if (countZero >= 3)
return 1;
return 0;
}
static int isHollow2 (int[] a) {
if (a.length < 3)
return 0;
int zeroCount = 0;
for (int i = 0, j = a.length - 1; i <= j; i++, j--) {
if ((a[i] == 0 && a[j] != 0) || (a[i] != 0 && a[j] == 0)) {
return 0;
}
if (i == j && a[i] == 0)
zeroCount++;
else if (i == j && a[i] != 0)
return 0;
else {
if (a[i] == 0)
zeroCount++;
if (a[j] == 0)
zeroCount++;
}
}
if (zeroCount >= 3) return 1;
return 0;
}
}