Python 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: Python # Python program to sort an array # in Zig-Zag form # Program for zig-zag conversion # of array def zigZag(arr, n): # Flag true indicates relation "<" # is expected, else ">" is expected. # The first expected relation is "<" flag = True for i in range(n - 1): # "<" relation expected if flag is True: # 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]: arr[i], arr[i + 1] = arr[i + 1], arr[i] # ">" 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]: arr[i], arr[i + 1] = arr[i + 1], arr[i] flag = bool(1 - flag) print(arr) # Driver code arr = [4, 3, 7, 8, 6, 2, 1] n = len(arr) zigZag(arr, n) # This code is contributed by Pratik Chhajer 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 Python Program For Converting Array Into Zig-Zag Fashion kartik Follow Improve Article Tags : Python Programs DSA Arrays Amazon Paytm +1 More Practice Tags : AmazonPaytmArrays Similar Reads Python Program for 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 Method 1:Approach of Python3 code This approach is simple. While travelling the matrix in the usual fashion, on basis of parity of 3 min read Python 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 2 min read Python Program to Print a given matrix in reverse spiral form Given a 2D array, print it in reverse spiral form. We have already discussed Print a given matrix in spiral form. This article discusses how to do the reverse printing. See the following examples. Input: 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: 1 2 5 min read Python3 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 arrayAlgorithm : 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 is 3 min read How to create an array of zeros in Python? Our task is to create an array of zeros in Python. This can be achieved using various methods, such as numpy.zeros(), which is efficient for handling large datasets. Other different methods are:Using the In-Build method numpy.zeros() methodUsing simple multiplication Using List comprehensionUsing it 5 min read How to Take Array Input in Python Using NumPy NumPy is a powerful library in Python used for numerical computing. It provides an efficient way to work with arrays making operations on large datasets faster and easier. To take input for arrays in NumPy, you can use numpy.array. Taking Array Input Using numpy.array()The most simple way to create 3 min read Python Program to Rotate Matrix Elements Given a matrix, clockwise rotate elements in it. Examples: Input 1 2 3 4 5 6 7 8 9 Output: 4 1 2 7 5 3 8 9 6 For 4*4 matrix Input: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Output: 5 1 2 3 9 10 6 4 13 11 7 8 14 15 16 12Recommended: Please solve it on âPRACTICE â first, before moving on to the solution. 3 min read Python - Matrix creation of n*n Matrices are fundamental structures in programming and are widely used in various domains including mathematics, machine learning, image processing, and simulations. Creating an nÃn matrix efficiently is an important step for these applications. This article will explore multiple ways to create such 3 min read Python3 Program to Inplace rotate square matrix by 90 degrees | Set 1 Given a square matrix, turn it by 90 degrees in anti-clockwise direction without using any extra space.Examples : Input:Matrix: 1 2 3 4 5 6 7 8 9Output: 3 6 9 2 5 8 1 4 7 The given matrix is rotated by 90 degree in anti-clockwise direction.Input: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Output: 4 8 12 4 min read Transpose Elements of Two Dimensional List - Python The task of transposing elements of a two-dimensional list in Python involves converting rows into columns and vice versa. This process rearranges the data structure so that the elements in the same position across different rows are grouped together in new sublists. For example, given the 2D list a 3 min read Like