-
Notifications
You must be signed in to change notification settings - Fork 2
/
Rearrange_the_Array.java
42 lines (40 loc) · 1.2 KB
/
Rearrange_the_Array.java
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
package com.company;
import java.io.IOException;
import java.util.Scanner;
public class Rearrange_the_Array {
public static void main(String[] args) throws IOException {
//write your code here
Scanner s = new Scanner(System.in);
int t = s.nextInt();
while (t-- > 0) {
int n = s.nextInt(), right = n - 1, left = 0;
int[] arr = new int[n];
int[] res = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = s.nextInt();
}
int count = 0;
for (int i = 0; i < n; i++) {
if (count == n)
break;
for (int k = n - 1; k >= 0; k--) {
res[count] = arr[right];
right--;
break;
}
count++;
if (count == n)
break;
for (int j = 0; j < n; j++) {
res[count] = arr[left];
left++;
break;
}
count++;
}
for(int i = 0; i < n; i++){
System.out.print(res[i]+" ");
}
}
}
}