-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathConvert_array_into_Zig-Zag_fashion.java
50 lines (44 loc) · 1.34 KB
/
Convert_array_into_Zig-Zag_fashion.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
43
44
45
46
47
48
49
50
import java.util.*;
import java.io.*;
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[] inputLine;
int n = Integer.parseInt(br.readLine().trim());
int[] arr = new int[n];
inputLine = br.readLine().trim().split(" ");
for (int i = 0; i < n; i++) {
arr[i] = Integer.parseInt(inputLine[i]);
}
new Solution().zigZag(arr, n);
for (int i = 0; i < n; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
}
}
}
class Solution {
void zigZag(int arr[], int n) {
boolean flag = true;
int temp = 0;
for(int i = 0; i<n-1; i++){
if( flag ){
if( arr[i] > arr[i+1] ){
temp = arr[i];
arr[i] = arr[i+1];
arr[i+1] = temp;
}
}else{
if( arr[i] < arr[i+1] ){
temp = arr[i];
arr[i] = arr[i+1];
arr[i+1] = temp;
}
}
flag = !flag;
}
}
}