# 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 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.