-
Notifications
You must be signed in to change notification settings - Fork 0
/
ArrayList: Separate Odd Even
53 lines (45 loc) · 1.23 KB
/
ArrayList: Separate Odd Even
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
import java.lang.*;
import java.util.*;
public class Main {
static void separateEvenOdd(ArrayList<Integer> a){
int aLen = a.size();
ArrayList<Integer> evenArr = new ArrayList<Integer>();
ArrayList<Integer> oddArr = new ArrayList<Integer>();
//System.out.println("length: "+aLen);
for(int i=0; i<aLen; i++){
if(a.get(i)%2 == 0){
evenArr.add(a.get(i));
}
else{
oddArr.add(a.get(i));
}
}
//printArray(a);
printArray(oddArr);
printArray(evenArr);
}
static void printArray(ArrayList<Integer> arr){
//System.out.println();
int arrLen = arr.size();
for(int i=0; i<arrLen; i++){
System.out.print(arr.get(i) + " ");
}
System.out.println();
}
public static void main(String[] args) {
// YOUR CODE GOES HERE
// Please take input and print output to standard input/output (stdin/stdout)
// DO NOT USE ARGUMENTS FOR INPUTS
// E.g. 'Scanner' for input & 'System.out' for output
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
for(int s=0; s<T; s++){
int n = sc.nextInt();
ArrayList<Integer> arr = new ArrayList<Integer>();
for(int i=0; i<n; i++){
arr.add(sc.nextInt());
}
separateEvenOdd(arr);
}
}
}