Shuffle the Array (LeetCode 1470): Solution and Explanation

Easy
Array

nums holds 2n numbers laid out as all the x values followed by all the y values. Return them interleaved as [x1,y1,x2,y2,...,xn,yn].

Intuition

Picture nums as a deck of cards cut exactly in half. The first n cards form the x pile and the last n cards form the y pile. The output is what you get from a perfect riffle shuffle: one card from the x pile, then one from the y pile, and so on.

Because the cut is exactly in the middle, the partner of the card at position i sits at position i + n. There is nothing to search for; the index arithmetic tells you where every card comes from.

So walk i from 0 to n - 1 and deal two cards each time: nums[i] lands at position 2i and nums[i + n] lands at position 2i + 1.

Approach: Deal from both halves

  1. Create a result array (or list) for 2n numbers.
  2. For i from 0 to n - 1, place nums[i] at position 2i.
  3. Place nums[i + n] at position 2i + 1.
  4. Return the result.

Step-by-step example

nums = [2,5,1,3,4,7], n = 3
  1. Cut the deck: the x pile is [2,5,1] and the y pile is [3,4,7].
  2. i = 0: deal 2 then 3. result = [2,3].
  3. i = 1: deal 5 then 4. result = [2,3,5,4].
  4. i = 2: deal 1 then 7. result = [2,3,5,4,1,7].
  5. Both piles are empty, so the answer is [2,3,5,4,1,7].

Code: Python, Java, C++

1class Solution:
2    def shuffle(self, nums: List[int], n: int) -> List[int]:
3        result = []
4        for i in range(n):
5            result.append(nums[i])
6            result.append(nums[i + n])
7        return result
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

O(n)

The loop runs n times and places two numbers each time.

Space

O(n)

The output holds 2n numbers. Beyond the output, only the loop index is used.

Other approaches

Fill by output index (time O(n), space O(n))

Loop over output positions k from 0 to 2n - 1 and read nums[k / 2] when k is even or nums[n + k / 2] when k is odd. Same work, just driven by the destination instead of the source.

In place with bit packing (time O(n), space O(1))

Since every value is at most 1000, which fits in 10 bits, you can store each element's final value in the upper bits of its destination slot during one pass, then shift everything down in a second pass. It avoids a second array but is much harder to read.

Common mistakes

  • Pairing nums[i] with nums[i + 1] instead of nums[i + n]. The partner is a full half away, not the next element.
  • Looping i up to 2n. The loop should run n times, because each iteration consumes one card from each pile.
  • Writing into nums directly without extra storage. Overwriting position 1 with y1 destroys x2 before you have copied it.

Frequently asked questions

Why is this called a shuffle if the result is not random?

It mirrors a perfect riffle shuffle of a deck of cards, where the two halves interleave exactly one card at a time. The order is fully determined by n.

Can it be done in O(1) extra space?

Yes, using the bounded values. Each number fits in 10 bits, so you can pack the target value into the high bits of each slot and unpack it afterwards. There are also cycle-following methods that work without that bound, but they are much more complex.

Does the input value range matter for the simple solution?

No. The straightforward version only moves values around, so it works for any integers. The range only matters for the bit packing trick.