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 element, so on. In short, the order of elements in the output zig-zag array would be [1st < 2nd > 3rd < 4th > 5th < 6th > 7th]. Therefore, if the given input array is [4, 3, 7, 8, 6, 2, 1], one of the arrangement in zig-zag array would be [3, 7, 4, 8, 2, 6, 1].

Few points worth mentioning here before we proceed towards thinking how to solve it efficiently. To start with, let us assume that all the elements in the input array are unique i.e. integers aren’t repeated. Later we can include repeated integers as well and extend the problem/solution. At first, finding zig-zag array problem looks similar to sorting but it should be noted that it’s not strict sorting. It means that this problem can have multiple outputs i.e. for the same input array, multiple zig-zag arrays can be found i.e. more than one solutions. Another point to be noted is that zig-zag array could be [1st > 2nd < 3rd > 4th < 5th > 6th < 7th]. Basically, what matters is the zig-zag relation of lesser than (<) and greater than (<) in the output not the starting order of first two elements. Basically a zig-zag is /\/\/\/\/\/ or \/\/\/\/\/\. Since arranging an array in zig-zag fashion is similar to sorting, this is the first approach which comes to mind! Therefore, first we can sort the array in increasing order and later we can start swapping elements (excluding the very first element). Let us understand it with example. Input array is [4, 3, 7, 8, 6, 2, 1]. Sorted array would be [1, 2, 3, 4, 6, 7, 8]. And if swap 2nd & 3rd elements, swap 4th & 5th elements, swap 6th & 7th elements, the output zig-zag array would be [1, 3, 2, 6, 4, 8, 7]. Here we can see {1 < 3 > 2 < 6 > 4 < 8 > 7}. Since time complexity of sorting is O(nlogn), this approach of converting into zig-zag results in O(nlogn). Let us see if we can improve here i.e. whether first sorting is really required.

If we think a little more, we notice that swapping elements in pair alone can result in zig-zag array. We actually need to traverse the array only once. While traversing the array, we can set the required order (i.e. either < or >) by swapping the elements if already not in the required order. To achieve this in a program, let us maintain a flag for representing which order (i.e. < or >) is needed. If the current two elements are not in that order then swap those elements otherwise not. Let us dig on how this works. Suppose we are processing B and C currently and the current relation is ‘<‘ but we have B > C in the input [A B C] – Since current relation is ‘<‘ it means that previous relation would be ‘>’. 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 got the desired order i.e. [A < C > B]. Since we are traversing array only once, time complexity is O(n)

Refer this for C++ implementation.

This article is contributed by Siva Krishna Aleti. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Convert array into Zig-Zag fashion
- Length of the longest ZigZag subarray of the given array
- Rearrange Odd and Even values in Alternate Fashion in Ascending Order
- Arrange N elements in circular fashion such that all elements are strictly less than sum of adjacent elements
- Count non-adjacent subsets from numbers arranged in Circular fashion
- Sort array after converting elements to their squares
- Minimum product of k integers in an array of positive Integers
- Count of primes after converting given binary number in base between L to R
- Mode in a stream of integers (running integers)
- Lexicographically smallest permutation of size A having B integers exceeding all preceeding integers
- Partition an array of non-negative integers into two subsets such that average of both the subsets is equal
- Split N into two integers whose addition to A and B makes them equal
- Minimum product pair an array of positive Integers
- Longest sequence of positive integers in an array
- Check if array contains contiguous integers with duplicates allowed
- Sum of f(a[i], a[j]) over all pairs in an array of n integers
- Print all distinct integers that can be formed by K numbers from a given array of N numbers
- Find integers that divides maximum number of elements of the array
- Ways to form an array having integers in given range such that total sum is divisible by 2
- Given an array and two integers l and r, find the kth largest element in the range [l, r]