Javascript Program For Converting Array Into Zig-Zag Fashion
Last Updated :
30 Aug, 2024
Given an array of DISTINCT elements, rearrange the elements of array in zig-zag fashion in O(n) time. The converted array should be in form a < b > c < d > e < f.
Example:
Input: arr[] = {4, 3, 7, 8, 6, 2, 1}
Output: arr[] = {3, 7, 4, 8, 2, 6, 1}
Input: arr[] = {1, 4, 3, 2}
Output: arr[] = {1, 4, 2, 3}
A Simple Solution is to first sort the array. After sorting, exclude the first element, swap the remaining elements in pairs. (i.e. keep arr[0] as it is, swap arr[1] and arr[2], swap arr[3] and arr[4], and so on).
Time complexity: O(N log N) since we need to sort the array first.
We can convert in O(n) time using an efficient approach. The idea is to use a modified one pass of bubble sort.
- Maintain a flag for representing which order(i.e. < or >) currently we need.
- If the current two elements are not in that order then swap those elements otherwise not.
Let us see the main logic using three consecutive elements A, B, C.
Suppose we are processing B and C currently and the current relation is '<', but we have B > C. Since current relation is '<' previous relation must be '>' i.e., A must be greater than B. So, the relation is A > B and B > C. We can deduce A > C. So if we swap B and C then the relation is A > C and C < B. Finally we get the desired order A C B
Refer this for more explanation.
Below image is a dry run of the above approach:

Below is the implementation of above approach:
JavaScript
// JavaScript program to sort an array
// in Zig-Zag form
// Program for zig-zag conversion
// of array
function zigZag(arr, n) {
// Flag true indicates relation "<"
// is expected, else ">" is expected.
// The first expected relation is "<"
let flag = true;
for (let i = 0; i <= n - 2; i++) {
// "<" relation expected
if (flag) {
// If we have a situation like
// A > B > C, we get A > B < C
// by swapping B and C
if (arr[i] > arr[i + 1])
temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
}
// ">" relation expected
else {
// If we have a situation like
// A < B < C, we get A < C > B
// by swapping B and C
if (arr[i] < arr[i + 1])
temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
}
// Flip flag
flag = !flag;
}
}
// Driver code
let arr = [4, 3, 7,
8, 6, 2, 1];
let n = arr.length;
zigZag(arr, n);
for (let i = 0; i < n; i++)
console.log(arr[i] + " ");
// This code is contributed by Surbhi Tyagi.
Complexity Analysis:
- Time complexity: O(n)
- Auxiliary Space: O(1)
Please refer complete article on Convert array into Zig-Zag fashion for more details!
Similar Reads
Convert Array into Zig-Zag fashion Given an array of distinct elements of size N, the task is to rearrange the elements of the array in a zig-zag fashion, i.e., the elements are arranged as smaller, then larger, then smaller, and so on. There can be more than one arrangement that follows the form: arr[0] < arr[1] > arr[2] <
12 min read
Javascript Program For Rearranging A Linked List In Zig-Zag Fashion Given a linked list, rearrange it such that the converted list should be of the form a < b > c < d > e < f ⦠where a, b, c⦠are consecutive data nodes of the linked list.Examples: Input: 1->2->3->4Output: 1->3->2->4 Explanation: 1 and 3 should come first before 2 and
5 min read
Javascript Program to Print matrix in antispiral form Given a 2D array, the task is to print matrix in anti spiral form:Examples: Output: 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1Input : arr[][4] = {1, 2, 3, 4 5, 6, 7, 8 9, 10, 11, 12 13, 14, 15, 16};Output : 10 11 7 6 5 9 13 14 15 16 12 8 4 3 2 1Input :arr[][6] = {1, 2, 3, 4, 5, 6 7, 8, 9, 10, 11, 12 13,
2 min read
Rearrange a Linked List in Zig-Zag fashion Given a linked list, rearrange it such that the converted list should be of the form a < b > c < d > e < f ⦠where a, b, c⦠are consecutive data nodes of the linked list. Examples: Input: 1->2->3->4 Output: 1->3->2->4 Explanation : 1 and 3 should come first before 2
15+ min read
Javascript Program For Sorting An Array Of 0s, 1s and 2s Given an array A[] consisting 0s, 1s and 2s. The task is to write a function that sorts the given array. The functions should put all 0s first, then all 1s and all 2s in last.Examples:Input: {0, 1, 2, 0, 1, 2}Output: {0, 0, 1, 1, 2, 2}Input: {0, 1, 1, 0, 1, 2, 1, 2, 0, 0, 0, 1}Output: {0, 0, 0, 0, 0
5 min read
JavaScript Program to Print Given Matrix in Spiral Form Write a JavaScript program to print a given 2D matrix in spiral form. You are given a two-dimensional matrix of integers. Write a program to traverse the matrix starting from the top-left corner which moves right, bottom, left, and up in a spiral pattern until all the elements are visited. Let us un
11 min read