Domain 1EasyCORE

Shuffle the Array

The problem requires us to rearrange elements in an array that is structured as `[x1,x2,...,xn,y1,y2,...,yn]` into a new form `[x1,y1,x2,y2,...,xn,yn]`. The input is an array `nums` of length `2n`, where the first half contains `x` elements and the second half contains `y` elements. The output should be a new array with elements interleaved from the two halves.

Domain: Easy•Updated: 2026-08-31
#Algorithms#LeetCode#qwen2.5-coder:14b#Array

1470. Shuffle the Array

Model: qwen2.5-coder:14b
Topics: Array
FAANG asked: 1
Acceptance rate: 88.2%


Problem Breakdown

The problem requires us to rearrange elements in an array that is structured as [x1,x2,...,xn,y1,y2,...,yn] into a new form [x1,y1,x2,y2,...,xn,yn]. The input is an array nums of length 2n, where the first half contains x elements and the second half contains y elements. The output should be a new array with elements interleaved from the two halves.

Inputs:

  • An integer array nums of length 2n.
  • An integer n representing the midpoint of the array.

Outputs:

  • A new integer array where elements are interleaved from the first and second halves of the input array.

Implicit Constraints or Tricky Edge Cases:

  • The array length is always even (2n).
  • Elements can be any integers between 1 and 1000.
  • No empty arrays are provided as input since 1 <= n <= 500.

Intuition

A brute-force approach would involve creating a new array and iterating through the first half of the input array, appending each element followed by its corresponding element from the second half. This approach is straightforward but has a time complexity of O(n) and space complexity of O(n), which is acceptable given the constraints.

However, a more efficient approach can be considered if we want to minimize additional space usage. We could try to rearrange elements in place, but this would require careful handling to avoid overwriting elements prematurely.

The key insight here is that since the array length is even and we know exactly where each element should go in the final array, we can directly map indices from the original array to their new positions without needing additional storage for intermediate results.

Approach

To solve this problem efficiently, we will use a two-pointer technique. We’ll iterate through the first half of the array and place each element followed by its corresponding element from the second half into a new result array. This approach ensures that we maintain O(n) time complexity while using O(n) space for the output array.

  1. Initialize an empty result array.
  2. Iterate over the range 0 to n-1.
  3. For each index i, append nums[i] followed by nums[i+n] to the result array.
  4. Return the result array.

Complexity Analysis

  • Time Complexity: O(n), where n is half the length of the input array. We iterate through the first half of the array once, performing constant-time operations for each element.
  • Space Complexity: O(n), as we create a new array to store the shuffled elements.

Solution

def shuffle(nums, n):
    result = []
    for i in range(n):
        result.append(nums[i])
        result.append(nums[i + n])
    return result

Edge Cases Handled

  • Minimum Length: The function handles arrays with a minimum length of 2 (n=1), where the input is [x, y].
  • Maximum Length: The function handles arrays with a maximum length of 1000 (n=500), where the input is [x1,x2,...,x500,y1,y2,...,y500].
  • Uniform Elements: The function correctly interleaves elements even if they are all the same, e.g., [1, 1, 1, 1] with n=2 results in [1, 1, 1, 1].

This solution is efficient and straightforward, adhering to the constraints and handling all edge cases appropriately.