-
Notifications
You must be signed in to change notification settings - Fork 0
/
ZigzagArray.java
60 lines (52 loc) · 1.42 KB
/
ZigzagArray.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
51
52
53
54
55
56
57
58
59
60
import java.util.*;
import java.util.Arrays;
import java.util.Collections;
import java.util.Scanner;
public class Pep_JavaIP_1Arrays_5Zigzag_array {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int[] arr = new int[scn.nextInt()];
for(int i = 0; i < arr.length; i++) {
arr[i] = scn.nextInt();
}
// solve_naive(arr);
solve_optimized(arr);
}
private static void solve_naive(int[] arr) {
// Time complexity: O(nlogn)
//first sort the array and then swap from 1 index
// Arrays.sort(arr, Collections.reverseOrder());
for (int i = 1; i < arr.length - 1; i += 2) {
swap(arr,i, i + 1);
}
for(int val: arr) {
System.out.print(val + " ");
}
System.out.println();
}
private static void solve_optimized(int[] arr) {
// Time complexity: O(n)
boolean lesser_than = true;
for (int i = 0; i < arr.length - 1; i++) {
if (lesser_than) {
if (arr[i] > arr[i + 1]) {
swap(arr,i, i + 1);
}
} else {
if (arr[i] < arr[i + 1]) {
swap(arr,i, i + 1);
}
}
lesser_than = !lesser_than;
}
for(int val: arr) {
System.out.print(val + " ");
}
System.out.println();
}
private static void swap(int[] arr, int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}