-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquestion11.java
More file actions
27 lines (24 loc) · 865 Bytes
/
question11.java
File metadata and controls
27 lines (24 loc) · 865 Bytes
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
import java.util.*;
public class question11 {
public static void rearrange(int[] arr) {
LinkedList<Integer> pos = new LinkedList<>();
LinkedList<Integer> neg = new LinkedList<>();
for (int num : arr) {
if (num >= 0) pos.add(num);
else neg.add(num);
}
int i = 0, p = 0, n = 0;
boolean turnPositive = true;
while (p < pos.size() && n < neg.size()) {
arr[i++] = turnPositive ? pos.get(p++) : neg.get(n++);
turnPositive = !turnPositive;
}
while (p < pos.size()) arr[i++] = pos.get(p++);
while (n < neg.size()) arr[i++] = neg.get(n++);
}
public static void main(String[] args) {
int[] arr = {1, 2, 3, -4, -1, 4};
rearrange(arr);
System.out.println(Arrays.toString(arr));
}
}