C++ Program For Converting Array Into Zig-Zag Fashion Last Updated : 09 Dec, 2022 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} Recommended: Please solve it on “PRACTICE ” first, before moving on to the solution. 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: C++ // C++ program to sort an array in // Zig-Zag form #include <iostream> using namespace std; // Program for zig-zag conversion // of array void zigZag(int arr[], int n) { // Flag true indicates relation "<" // is expected, else ">" is expected. // The first expected relation is "<" bool flag = true; for (int 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]) swap(arr[i], arr[i+1]); } /* ">" 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]) swap(arr[i], arr[i+1]); } // flip flag flag = !flag; } } // Driver code int main() { int arr[] = {4, 3, 7, 8, 6, 2, 1}; int n = sizeof(arr) / sizeof(arr[0]); zigZag(arr, n); for (int i = 0; i < n; i++) cout << arr[i] << " "; return 0; } Output: 3 7 4 8 2 6 1 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 C++ Program For Converting Array Into Zig-Zag Fashion kartik Follow Improve Article Tags : C++ Programs DSA Arrays Amazon Paytm +1 More Practice Tags : AmazonPaytmArrays Similar Reads C++ Program to Print matrix in zag-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 9 Approach of C++ code The approach is simple. Just simply iterate over every diagonal elements one at a time and change the directio 3 min read C++ 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 1 Input : 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 1 Input :arr[][6] = {1, 2, 3, 4, 5, 6 7, 8, 9, 10, 11, 12 3 min read C++ Program for Block swap algorithm for array rotation Write a function rotate(ar[], d, n) that rotates arr[] of size n by d elements. Rotation of the above array by 2 will make array Algorithm : Initialize A = arr[0..d-1] and B = arr[d..n-1] 1) Do following until size of A is equal to size of B a) If A is shorter, divide B into Bl and Br such that Br i 4 min read Program to print an array in Pendulum Arrangement with constant space Given an array arr[] of integers, the task is to arrange them in a way similar to the to-and-fro movement of a Pendulum without using any extra space.Pendulum Arrangement: The minimum element out of the list of integers must come in the center position of the array.The number in the ascending order 12 min read C++ Program to Print the Pattern 'G" In this article, we will learn how to print the pattern G using stars and white spaces. Given a number n, we will write a program to print the pattern G over n lines or rows.Examples: Input : 7 Output : *** * * * *** * * * * *** Input : 9 Output : ***** * * * * *** * * * * * * ***** In this program, 2 min read C++ Program for Reversal algorithm for right rotation of an array Given an array, right rotate it by k elements. After K=3 rotation Examples: Input: arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} k = 3 Output: 8 9 10 1 2 3 4 5 6 7 Input: arr[] = {121, 232, 33, 43 ,5} k = 2 Output: 43 5 121 232 33 Note : In the below solution, k is assumed to be smaller than or equal to n 2 min read C++ Program To Print Inverted Hollow Star Pyramid Pattern Given the value of R(number of rows), write a C++ program to print the Inverted Hollow Pyramid using stars and white spaces. Examples: Input: R = 5 Output: ********* * * * * * * * Input: R = 10 Output: ******************* * * * * * * * * * * * * * * * * * Algorithm: At first, take the number of rows 2 min read Program to print the given Z Pattern Given an integer N, the task is to print the Alphabet Z Pattern as given below: 1 2 3 * * * N * * * 3 2 1 2 3 * * * N Examples: Input: N = 5 Output: 1 2 3 4 5 4 3 2 1 2 3 4 5 Input: N = 4 Output: 1 2 3 4 3 2 1 2 3 4 Recommended: Please try your approach on {IDE} first, before moving on to the soluti 7 min read Array C/C++ Programs C Program to find sum of elements in a given arrayC program to find largest element in an arrayC program to multiply two matricesC/C++ Program for Given an array A[] and a number x, check for pair in A[] with sum as xC/C++ Program for Majority ElementC/C++ Program for Find the Number Occurring Odd N 6 min read C++ Program for Mirror of matrix across diagonal Given a 2-D array of order N x N, print a matrix that is the mirror of the given tree across the diagonal. We need to print the result in a way: swap the values of the triangle above the diagonal with the values of the triangle below it like a mirror image swap. Print the 2-D array obtained in a mat 4 min read Like