1470. Shuffle the Array

Prompt

You’re given an array called nums that is composed of 2n elements in the following format:

[x1,x2,...,xn,y1,y2,...,yn]
[x1,y1,x2,y2,...,xn,yn]
Input: nums = [2,5,1,3,4,7], n = 3
Output: [2,3,5,4,1,7]

Approach

The approach for this problem can be fairly straightforward. You could first instantiate an empty array that we will eventually return as the final output array (in my case, I named it res).

res = []
for i in range(n):
res.append(nums[i])
res.append(nums[i + n])

Code

Python

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
John Kim

John Kim

75 Followers

iOS Developer | Full Stack Developer | Software Engineer | LinkedIn: john-kim-developer | GitHub: cloudiosx | Portfolio: cloudiosx.com