```
public int gcd(int n, int k) {
return n%k==0?k:gcd(k, n%k);
}
public void rotate(int[] nums, int k) {
int n=nums.length;
k%=n;
if (k==0) return;
int g=gcd(n, k);
for (int i=0;i<g;i++) {
int j=i, l=(i+n-k)%n, tmp=nums[i];
while (l!=i) {
nums[j]=nums[l];
j=l;l=(l+n-k)%n;
}
nums[j]=tmp;
}
}
```