//Time complexity O(n), extra space cost O(1)

```
public class Solution {
public void rotate(int[] nums, int k) {
k %= nums.length;
if(k<=0) return;
int index = k, loopHead = 0, curr = nums[0];
//1. index is the index of the element we will update in each iteration.
//2. loopHead is head of a loop (if exist) for index since each time we
// move index k steps further.
//3. curr is the value got from previous iteration nums[preIndex] and
//for updating nums[index]
for(int count=0;count<nums.length;count++){
if(index==loopHead){ //loop detected
nums[index] = curr; //set the value of loopHead.
loopHead++; //move 1 step further to jump out of loop
curr = nums[++index]; //This is the head of new loop
index = (index+k)%nums.length;
}
else{ //each time go k steps further
int tmp = nums[index];
nums[index] = curr;
curr = tmp;
index = (index+k)%nums.length;
}
}
}
}
```