array and hashmap January 10, 2023

Shuffle the array

Time O(n) Space O(1) Open original problem

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)