Last active
January 6, 2024 17:19
-
-
Save k0ff33/3029d30fb4ae62a72b0c54f2cb39893d to your computer and use it in GitHub Desktop.
Cyclic Rotation Exercise from Codility (JavaScript/NodeJS)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Shift items in the array to the right by number of indexes | |
* @param {array} A | |
* @param {int} K | |
* @return {array} | |
*/ | |
function solution(A, K) { | |
// if A is meant to be shifted by its own length (K) then just return the array | |
if (A.length === K || K === 0) { | |
return A | |
} | |
// Run K number of times saving last element in the array as a temporary variable, adding it to the front of the array and removing the last element | |
for (let i = 0; i < K; i++) { | |
let lastElement = A[A.length - 1] | |
A.unshift(lastElement) | |
A.pop() | |
} | |
return A | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Solution from elendil7 fail performance tests