EasyNo. 1470

Shuffle the Array — LeetCode 1470 Python Solution

Topics
Array
Reading time
2 min

The problem

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

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

Python solution

PythonSolution
class Solution:
    def shuffle(self, nums: List[int], n: int) -> List[int]:
        return [x for pair in zip(nums[:n], nums[n:]) for x in pair]

Complexity

Time
O(n)
Space · auxiliary
O(n)

Related problems

Related problems
No.ProblemDifficulty
0057Insert IntervalMedium
0228Summary RangesEasy
0485Max Consecutive OnesEasy
0665Non-decreasing ArrayMedium
0674Longest Continuous Increasing SubsequenceEasy
0699Falling SquaresHard

Frequently asked questions

How hard is LeetCode 1470. Shuffle the Array?

LeetCode 1470. Shuffle the Array is rated Easy on LeetCode.

What is the time complexity of LeetCode 1470. Shuffle the Array?

The Python solution on this page runs in O(n).

What is the space complexity of LeetCode 1470. Shuffle the Array?

The Python solution on this page uses O(n) auxiliary space.

What topics does LeetCode 1470. Shuffle the Array cover?

LeetCode 1470. Shuffle the Array is tagged Array on LeetCode.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity, invisible to screen sharing.

Free to download. Check it runs on your machine, then pick a plan.