Normalize with modulo08.05
When k can be huge, rotating k times is wasteful. Positions wrap every n, so k and k%n are the same rotation.
Use ((k % n) + n) % n so negative k still lands in 0..n-1.
Same skill, three traces. Read all three before you try it.
01k bigger than n - given
n = 4,k = 5- walk
5%4=1rotate by
1, not5- result
- same as
k=1
02k multiple of n - given
n = 4,k = 8- walk
8%4=0identity
- result
- unchanged
03negative k - given
n = 4,k = -1- walk
-
1mod4=3left -
1= right1- result
- normalize before rotating
normK(k, n).k is an integer. n is the array length.Return ((
k % n) + n) % n so the result is in 0..n-1.For
n=4: k=5 → 1, k=8 → 0, k=-1 → 3, k=0 → 0. Then left-rotate [4, 1, 9, 1] by that normalized k.constraintsa is non-empty. n = a.length. k is an integer.
- Rotate Array0/3 skills ready · arrays · mediumopen
- Rotate Array0/3 skills ready · arrays · mediumopen
Every Check with Awel is frozen here. Open one to read the code or load it back into the editor.