-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathWaveArray.java
35 lines (32 loc) · 1.2 KB
/
WaveArray.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
package SummerTrainingGFG.Arrays;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* @author Vishal Singh */
public class WaveArray {
public static void main(String[] args)throws IOException {
/*
* Given a sorted array arr[] of distinct integers. Sort the array into a wave-like array and return it.
* In other words, arrange the elements into a sequence such that a1 >= a2 <= a3 >= a4 <= a5..... (considering the increasing lexicographical order).
* */
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine());
while(t-- > 0){
int n = Integer.parseInt(br.readLine());
String[] str = br.readLine().split(" ");
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = Integer.parseInt(str[i]);
}
for (int i = 0; i < n-1; i+=2) {
int temp = arr[i];
arr[i] = arr[i+1];
arr[i+1] = temp;
}
for (int i = 0; i < n; i++) {
System.out.print(arr[i]+" ");
}
}
}
}