-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDay93.java
More file actions
45 lines (37 loc) · 1.11 KB
/
Day93.java
File metadata and controls
45 lines (37 loc) · 1.11 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
import java.util.Arrays;
public class Day93 {
public static int[] alternatePositiveNegative(int[] A, int N) {
int[] result = new int[N];
int positiveIndex = 0;
int negativeIndex = 0;
while (positiveIndex < N && A[positiveIndex] < 0) {
positiveIndex++;
}
while (negativeIndex < N && A[negativeIndex] > 0) {
negativeIndex++;
}
int i = 0;
while (i < N && positiveIndex < N && negativeIndex < N) {
if (i % 2 == 0) {
result[i++] = A[positiveIndex++];
} else {
result[i++] = A[negativeIndex++];
}
}
while (positiveIndex < N) {
result[i++] = A[positiveIndex++];
}
while (negativeIndex < N) {
result[i++] = A[negativeIndex++];
}
return result;
}
public static void main(String[] args) {
int[] A = {1, 2, -4, -5};
int N = 4;
int[] result = alternatePositiveNegative(A, N);
for (int num : result) {
System.out.print(num + " ");
}
}
}