-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlternateArraySplit.java
More file actions
37 lines (30 loc) · 856 Bytes
/
Copy pathAlternateArraySplit.java
File metadata and controls
37 lines (30 loc) · 856 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
28
29
30
31
32
33
34
35
36
37
public class AlternateArraySplit {
public static void main(String[] args) {
int[] arr = {1,2,3,4,5,6,7,8,9,10};
int n = arr.length;
int xSize = (n + 1) / 2;
int ySize = n / 2;
int[] x = new int[xSize];
int[] y = new int[ySize];
int xi = 0;
int yi = 0;
for (int i = 0; i < n; i++) {
if (i % 2 == 0) {
x[xi] = arr[i];
xi = xi + 1;
} else {
y[yi] = arr[i];
yi = yi + 1;
}
}
System.out.print("x = ");
for (int i = 0; i < xSize; i++) {
System.out.print(x[i] + " ");
}
System.out.println();
System.out.print("y = ");
for (int i = 0; i < ySize; i++) {
System.out.print(y[i] + " ");
}
}
}