# Blogs Hub

### by Sumit Chourasia | Jun 07, 2020 | Category :coding | Tags : आसानc-sharpडेटा-संरचनाalgorithmarrayleetcode

#### Shuffle the Array - Easy - LeetCode - मिनी टीवी

Given the array nums consisting of 2n elements in the form [x1,x2,...,xn,y1,y2,...,yn].

Return the array in the form [x1,y1,x2,y2,...,xn,yn].

Example 1:

Input: nums = [2,5,1,3,4,7], n = 3

Output: [2,3,5,4,1,7]

Explanation: Since x1=2, x2=5, x3=1, y1=3, y2=4, y3=7 then the answer is [2,3,5,4,1,7].

Example 2:

Input: nums = [1,2,3,4,4,3,2,1], n = 4

Output: [1,4,2,3,3,2,4,1]

Example 3:

Input: nums = [1,1,2,2], n = 2

Output: [1,2,1,2]

Constraints:

1 <= n <= 500

nums.length == 2n

1 <= nums[i] <= 10^3

Solution:

``````using System;
using System.Collections.Generic;
using System.Text;

{
public class ShuffleSoln
{
public int[] Shuffle(int[] nums, int n)
{
var res = new int[nums.Length];
var index = 0;
for (int i = 0, j = n; j < nums.Length; i++, j++)
{
res[index] = nums[i];
index++;
res[index] = nums[j];
index++;
}

return res;
}
}
}
``````

Time Complexity: O(n)

Space Complexity: O(n)

Unit Tests:

``````using LeetCode.AskGif.Easy.Array;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Text;

namespace CodingUnitTest.Easy.Array
{
[TestClass]
public class ShuffleSolnTests
{
[TestMethod]
public void ShuffleSoln_First()
{
var nums = new int[] { 2, 5, 1, 3, 4, 7 };
var n = 3;
var output = new int[] { 2, 3, 5, 4, 1, 7 };
var res = new ShuffleSoln().Shuffle(nums, n);

AreEqual(res, output);
}

[TestMethod]
public void ShuffleSoln_Second()
{
var nums = new int[] { 1, 2, 3, 4, 4, 3, 2, 1 };
var n = 4;
var output = new int[] { 1, 4, 2, 3, 3, 2, 4, 1 };
var res = new ShuffleSoln().Shuffle(nums, n);

AreEqual(res, output);
}

[TestMethod]
public void ShuffleSoln_Third()
{
var nums = new int[] { 1, 1, 2, 2 };
var n = 2;
var output = new int[] { 1, 2, 1, 2 };
var res = new ShuffleSoln().Shuffle(nums, n);

AreEqual(res, output);
}

private void AreEqual(int[] res, int[] output)
{
Assert.AreEqual(res.Length, output.Length);

for (int i = 0; i < res.Length; i++)
{
Assert.AreEqual(res[i], output[i]);
}
}
}
}
``````

Contributed By: Sumit Chourasia
Contributed By: Sumit Chourasia
Contributed By: Sumit Chourasia
Contributed By: Sumit Chourasia