LeetCode Entry
1470. Shuffle the Array
For simplicity, use two pointers for the source, and one for the destination.
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)\)