
Leetcode Problem 189: Rotate Array
Given an array, rotate the array to the right by k steps, where k is non-negative.
k elementsfunction rotate(nums: number[], k: number): void {
const kModulo = k % nums.length
for(let i = 0, j = nums.length - 1; i < j; i++, j--) {
[nums[i], nums[j]] = [nums[j], nums[i]]
}
for(let i = 0, j = kModulo - 1; i < j; i++, j--) {
[nums[i], nums[j]] = [nums[j], nums[i]]
}
for(let i = kModulo, j = nums.length - 1; i < j; i++, j--) {
[nums[i], nums[j]] = [nums[j], nums[i]]
}
}
const example = [1,2,3,4,5,6,7]
rotate(example, 3)
console.log(example)