IT General

Codility #2-CyclicRotation

SoftArts 2018. 8. 18. 18:18
function solution(A, K) {
let arr = Array.from(A); // clone
let n = K % A.length; // for better performance

for (let i = 0; i < n; i++) {
let val = arr.pop();
arr.unshift(val);
}

return arr;
}

function solution(A, K) {
let arr = [];

for (let i = 0, j; i < A.length; i++) {
j = (i + K) % A.length;
arr[j] = A[i];
}

return arr;
}