Warm-ups · 5 of 24
Left-rotate an array by k
Return the array rotated left by k positions: the first k elements move to the end, in order. k can be larger than the length.
Examples
Input nums = [1, 2, 3, 4, 5], k = 2
Output [3, 4, 5, 1, 2]
Input nums = [1, 2, 3], k = 4
Output [2, 3, 1]
Rotating by 3 changes nothing, so k = 4 is the same as k = 1.
📘 New to this? Building a new string or array as the answer explains the idea in a few minutes.
Solved it? The same idea on LeetCode: try it there ↗