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

## Recommended Posts:

- Convert array into Zig-Zag fashion
- Minimum product of k integers in an array of positive Integers
- Sort array after converting elements to their squares
- Factorial of an Array of integers
- Sum of f(a[i], a[j]) over all pairs in an array of n integers
- Find the first repeating element in an array of integers
- Print all the peaks and troughs in an Array of Integers
- Longest sequence of positive integers in an array
- Find a pair with maximum product in array of Integers
- Move all zeros to start and ones to end in an Array of random integers
- Minimum product pair an array of positive Integers
- Check if array contains contiguous integers with duplicates allowed
- Check if array contains contiguous integers with duplicates allowed
- Minimum increments to convert to an array of consecutive integers
- Given an array and two integers l and r, find the kth largest element in the range [l, r]