The algorithm for this should be very simple, like:
aux <- array[array.Length - 1]
for index = 0, array.Length - 2 do
array[index + 1] <- array[index]
array[0] <- aux
endfor
Note, that in the special cases when array.Length <= 1
, you do not need anything to achieve the rotation. If you want to achieve K rotations without an array, you can call this K times.
You will need to be tricky to achieve an optimal algorithm. Here I will take the following approach. An array's given element can have three different possible states. I explain it through the example. If I put the K'th element into a variable called aux and place the 0'th element in its place, then we have the following three states:
- at element 0 the original element was already moved to another place, but the final element did not arrive yet. This is the moved state
- at element K the original element was already moved and the final element already arrived there. This is the arrived state
- at element 2 * K we did nothing so far, so there we have the original state
So, if we can mark somehow the elements, then the algorithm would look like this:
arrivedCount <- 0 //the number of arrived elements is counted in order to make sure we know when we need to search for an element with an original state
index <- 0
N <- array.Length
aux <- array[index]
mark(array[index], moved)
index <- (index + K) mod N
while arrivedCount < N do
state <- array[index]
if (state = moved) then
array[index] <- aux
arrivedCount <- arrivedCount + 1
mark(array[index], arrived)
if arrivedCount < N then
while (state(array[index]) <> original) do
index <- (index + 1) mod N
endwhile
aux <- array[index]
mark(array[index], moved)
index <- (index + K) mod N
endif
else //it is original
aux2 <- array[index]
array[index] <- aux
aux <- aux2
arrivedCount <- arrivedCount + 1
mark(array[index], arrived)
index <- (index + K) mod N
endif
endwhile
Now, how could we use this in practice? Let's consider the example when your array only has positive numbers as value. You mark all elements at start by assigning them their negative value (-5 instead of 5, for example). Whenever a state is modified to move, it will have a value of 0 and whenever it is arrived, you will have the positive number. It is up to you to define how you can mark such elements and you will need to do this in conformity with your task. If you are unable to mark the elements for any reason, then you will need to create an auxiliary array in order to solve this.
EDIT
Do not be afraid of the while, it should not search for too many steps because of the modulo classes. An implementation in Javascript:
var array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
var arrivedCount = 0;
var index = 0;
var N = array.length;
var K = 3;
for (var i = 0; i < array.length; i++) array[i] = -array[i];
var aux, aux2, state;
aux = array[index];
array[index] = 0;
index = (index + K) % N;
var step = 0
while ((arrivedCount < N) && (++step < 1000)) {
if (array[index] === 0) {
array[index] = -aux;
arrivedCount++;
if (arrivedCount < N) {
while (array[index] >= 0) index = (index + 1) % N;
aux = array[index];
array[index] = 0;
index = (index + K) % N;
}
} else {
aux2 = array[index];
array[index] = -aux;
aux = aux2;
arrivedCount++;
index = (index + K) % N
}
}
Change the definition of array and K according to your will.