Javascript Program For Converting Array Into Zig-Zag Fashion Last Updated : 30 Aug, 2024 Comments Improve Suggest changes Like Article Like Report 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. Output3 7 8 6 2 1 4 Complexity Analysis:Time complexity: O(n) Auxiliary Space: O(1) Please refer complete article on Convert array into Zig-Zag fashion for more details! Comment More infoAdvertise with us Next Article Javascript Program For Converting Array Into Zig-Zag Fashion kartik Follow Improve Article Tags : JavaScript Web Technologies DSA Arrays Amazon Paytm +2 More Practice Tags : AmazonPaytmArrays Similar Reads Converting an array of integers into Zig-Zag fashion! Let us elaborate the problem a little more. Basically, we are given an array of integers and we need to arrange this array in an order such that 1st element is lesser than 2nd element, 2nd element is greater than 3rd element, 3rd element is lesser than 4th element, 4th element is greater than 5th el 4 min read 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 Print matrix in zig-zag fashion from the last column Given a matrix of 2-Dimensional array of n rows and n columns. Print this matrix in ZIG-ZAG fashion starting from column n-1 as shown in the figure below. Examples: Input: mat[][] = 1 2 3 4 5 6 7 8 9 Output: 3 2 6 9 5 1 4 8 7 Input: mat[][] = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Output: 4 3 8 12 7 12 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 Print Concatenation of Zig-Zag String in 'n' Rows Given a string and number of rows 'n'. Print the string formed by concatenating n rows when input string is written in row-wise Zig-Zag fashion. Examples: Input: str = "ABCDEFGH" n = 2 Output: "ACEGBDFH" Explanation: Let us write input string in Zig-Zag fashion in 2 rows. A C E G B D F H Now concate 12 min read Javascript Program For Rearranging An Array In Maximum Minimum Form - Set 2 (O(1) extra space) Given a sorted array of positive integers, rearrange the array alternately i.e first element should be the maximum value, second minimum value, third-second max, fourth-second min and so on. Examples:Input: arr[] = {1, 2, 3, 4, 5, 6, 7} Output: arr[] = {7, 1, 6, 2, 5, 3, 4}Input: arr[] = {1, 2, 3, 4 5 min read Print matrix in zig-zag fashion Given a matrix of 2D array of n rows and m columns. Print this matrix in ZIG-ZAG fashion as shown in figure. Example: Input: {{1, 2, 3}{4, 5, 6}{7, 8, 9}}Output: 1 2 4 7 5 3 6 8 9Input : [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]Output:: 1 2 5 9 6 3 4 7 10 13 14 11 8 12 15 16Thi 10 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 Array to ArrayList Conversion in Java In Java, arrays are fixed-sized, whereas ArrayLists are part of the Java collection Framework and are dynamic in nature. Converting an array to an ArrayList is a very common task and there are several ways to achieve it.Methods to Convert Array to an ArrayList1. Using add() Method to Manually add th 3 min read Like