k elements to the end of the array.k elements, then reverse the rest.k (the number of left rotations).k to the end.Here’s the code:
class Solution {
public:
void rotate(vector<int>& nums, int k) {
int n = nums.size();
k = k % n;
reverse(nums.begin(), nums.end());
// rotating rest of elements
reverse(nums.begin(), nums.begin() + k);
reverse(nums.begin() + k, nums.end());
}
};