Handle k greater than length08.07
If you forget to mod, index i-k can be wildly negative and crash. The array only has n distinct rotations.
Always reduce k before moving. k=n, 2n, 3n are all the identity rotation.
Same skill, three traces. Read all three before you try it.
01k=4, n=4 - given
a = [4, 1, 9, 1],k = 4- walk
4%4=0- result
[4, 1, 9, 1]
02k=6, n=4 - given
a = [4, 1, 9, 1],k = 6- walk
6%4=2left by
2- result
[9, 1, 4, 1]
03k=100 - given
a = [4, 1, 9, 1],n = 4,k = 100- walk
100%4=0- result
- identity, do not loop
100times
rotL(a, k).a is an array of integers. n is a.length. k is a non-negative integer and may be larger than n.Reduce
k with k % n, then left-rotate. Print k and k%n beside each result.On
[4, 1, 9, 1]: k=0 and k=4 and k=100 → [4, 1, 9, 1]. k=6 → [9, 1, 4, 1].constraintsa is non-empty. k ≥ 0.
- 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.