-
Notifications
You must be signed in to change notification settings - Fork 0
/
Alternate positive and negative numbers
64 lines (51 loc) · 1.55 KB
/
Alternate positive and negative numbers
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
//Alternate positive and negative numbers
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int tc = Integer.parseInt(br.readLine().trim());
while(tc-- > 0) {
String input = br.readLine();
String[] inputArray = input.split("\\s+");
ArrayList<Integer> arr = new ArrayList<>();
for(String s : inputArray) {
arr.add(Integer.parseInt(s));
}
new Solution().rearrange(arr);
for(int num : arr) {
System.out.print(num + " ");
}
System.out.println();
}
}
}
class Solution {
void rearrange(ArrayList<Integer> arr) {
List<Integer> A = new ArrayList<>();
List<Integer> B = new ArrayList<>();
for(int i = 0; i < arr.size(); i++) {
if(arr.get(i) < 0) {
A.add(arr.get(i));
}
else {
B.add(arr.get(i));
}
}
int i = 0, j = 0, k = 0;
while(i < A.size() && j < B.size()) {
if(k % 2 == 0) {
arr.set(k++, B.get(j++));
}
else {
arr.set(k++, A.get(i++));
}
}
while(i < A.size()) {
arr.set(k++, A.get(i++));
}
while(j < B.size()) {
arr.set(k++, B.get(j++));
}
}
}