DEV Community

loading...

LeetCode 1470. Shuffle the Array (javascript solution)

codingpineapple
・1 min read

Description:

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:

Time Complexity : O(n)
Space Complexity: O(n)

var shuffle = function(nums, n) {
    let res = []
    for(let i = 0; i < n; i++){
        res.push(nums[i], nums[i+n])
    }
    return res
};
Enter fullscreen mode Exit fullscreen mode

Discussion (0)