Shuffle the array

January 10, 2023

array-and-hashmap

Problem URL: Shuffle the array

We will iterate over the half of of the array and add the elements to the result array form the front and middle.

class Solution:
    def shuffle(self, nums: List[int], n: int) -> List[int]:
        res, n = [], len(nums)//2
        for i in range(n):
            res.extend([nums[i], nums[n+i]])
        return res

Time complexity: O(n)
Space complexity: O(1)