Skip to main content
Back to problems
#1470
Easy Algorithms

Shuffle the array

Array
88.9% acceptance
Feb 25, 2026
6356
349
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].

Solution

Rust
Time O(n)
Space O(n)
LeetCode
solution.rs
impl Solution {
  pub fn shuffle(nums: Vec<i32>, n: i32) -> Vec<i32> {
    let n = n as usize;
    let mut result = Vec::with_capacity(2 * n);
    for i in 0..n {
      result.push(nums[i]);
      result.push(nums[i + n]);
    }
    result
  }
}