Rotate an array to the left by k positions (simulating deque rotation). Elements shifted off the front are appended to the back.
Example Input & Output
Example 1
Input
[1],3
Output
[1]
Explanation
Single element
Example 2
Input
[1,2,3],1
Output
[2,3,1]
Explanation
Rotate left by 1
Example 3
Input
[1,2,3,4,5],2
Output
[3,4,5,1,2]
Explanation
Rotate left by 2
Example 4
Input
[1,2,3],0
Output
[1,2,3]
Explanation
No rotation
Example 5
Input
[],5
Output
[]
Explanation
Empty
Algorithm Flow

Recommendation Algorithm Flow for Deque Rotate
Solution Approach
Best Answers
java
class Solution {
public int[] solution(int[] nums, int k) {
int n=nums.length;if(n==0)return new int[]{};k%=n;
int[] r=new int[n];
for(int i=0;i<n;i++)r[i]=nums[(i+k)%n];
return r;
}
}Comments (0)
Join the Discussion
Share your thoughts, ask questions, or help others with this Challenge.
