10
3
Write a function that rotates an integer array by a given number k. k elements from the end should move to the beginning of array, and all other elements should move to right to make the space.
The rotation should be done in-place.
Algorithm should not run in more than O(n), where n is the size of array.
Also a constant memory must be used to perform the operation.
For example,
if array is initialized with elements arr = {1, 2, 3, 4, 5, 6, 7, 8, 9}
rotate(arr, 3) will result the elements to be {7, 8, 9, 1, 2, 3, 4, 5, 6}
rotate(arr, 6) will result the {4, 5, 6, 7, 8, 9, 1, 2, 3}
1What is meant by constant memory here? Surely it requires at least O(n) memory at a minimum just to store the array being processed making O(1) memory usage impossible. – Post Rock Garf Hunter – 2017-08-22T17:09:30.620
2I'm voting to close this question as off-topic because questions without an objective primary winning criterion are off-topic, as they make it impossible to indisputably decide which entry should win. There is absolutely no reason that this should be a popularity contest. – James – 2017-08-22T17:36:42.587
Voted to close. From the [tag:popularity-contest] wiki (here), "Gives freedom to entrants to decide what to do in crucial parts and incentivizes them to use this freedom." I don't think leaving the challenge open to any algorithm counts as encouraging creativity for such a simple challenge, at least not to the extent that it works as a popcon. This would be more suited as a [tag:code-golf] challenge.
– mbomb007 – 2017-08-22T20:56:46.747