LeetCode Entry

1470. Shuffle the Array

6.02.2023 easy 2023 kotlin

For simplicity, use two pointers for the source, and one for the destination.

1470. Shuffle the Array easy

blog post

    fun shuffle(nums: IntArray, n: Int): IntArray {
        val arr = IntArray(nums.size)
        var left = 0
        var right = n
        var i = 0
        while (i < arr.lastIndex) {
            arr[i++] = nums[left++]
            arr[i++] = nums[right++]
        }
        return arr
    }

Telegram

https://t.me/leetcode_daily_unstoppable/110

Intuition

Just do what is asked.

Approach

For simplicity, use two pointers for the source, and one for the destination.

Complexity

  • Time complexity: \(O(n)\)
  • Space complexity: \(O(n)\)