Geeksforgeeks

Error

Given an array **Arr** (distinct elements) of size **N**. Rearrange the elements of array in zig-zag fashion. The converted array should be in form **a < b > c < d > e < f.** The relative order of elements is same in the output **i.e** you have to iterate on the original array only.

**Example 1:**

**Input:
**N = 7
Arr[] = {4, 3, 7, 8, 6, 2, 1}
**Output: **3 7 4 8 2 6 1
**Explanation:** 3 < 7 > 4 < 8 > 2 < 6 > 1

**Example 2:**

**Input:
**N = 4
Arr[] = {1, 4, 3, 2}
**Output:** 1 4 2 3
**Explanation: **1 < 4 > 2 < 3

**Your Task:**

You don't need to read input or print anything. Your task is to complete the function **zigZag()** which takes the array of integers **arr **and **n**** **as parameters and returns void. You need to modify the array itself.

**Expected Time Complexity:** O(N)

**Expected Auxiliary Space:** O(1)

**Constraints:**

1 <= N <= 10^{5}

0 <= Arr_{i} <= 10^{6}

We are replacing the old Disqus forum with the new Discussions section given below.

Click here to view old Disqus comments.

Click here to view old Disqus comments.

Login to report an issue on this page.

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

YesLoading...

Convert array into Zig-Zag fashion

...