Geeksforgeeks

Error

Given an array **arr[]** of **n** elements in the following format {a1, a2, a3, a4, ….., an/2, b1, b2, b3, b4, …., bn/2}, the task is shuffle the array to {a1, b1, a2, b2, a3, b3, ……, an/2, bn/2} without using extra space.

**Example 1:**

**Input: **n = 4, arr[] = {1, 2, 9, 15}
**Output:** 1 9 2 15
**Explanation**: a1=1 , a2=2 , b1=9 , b2=9
So the final array will be :
a1, b1, a2, b2 = { 1, 9, 2, 15 }
**Example 2:**
**Input: **n = 6
arr[] = {1, 2, 3, 4, 5, 6}
**Output:** 1 4 2 5 3 6

**Your Task:**

This is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function **shuffleArray**() that takes array** arr[], **and an integer** n** as parameters and modifies the given array according to the above-given pattern.

**Expected Time Complexity:** O(n).

**Expected Auxiliary Space:** O(n).

**Constraints:**

1 ≤ n ≤ 10^{5}

1≤ arr_{i} ≤ 10^{5}

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

Shuffle integers

...