LeetCode 1470. Shuffle the Array (javascript solution)

cod3pineapple

codingpineapple

Posted on June 6, 2021

LeetCode 1470. Shuffle the Array (javascript solution)

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
πŸ’– πŸ’ͺ πŸ™… 🚩
cod3pineapple
codingpineapple

Posted on June 6, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related