Code Logo

Deque Rotate

Published at25 Jul 2026
Deque Easy 0 views
Like0

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
Recommendation Algorithm Flow for Deque Rotate

Solution Approach

function solution(arr, k) {
  if (arr.length === 0) return [];
  k = k % arr.length;
  return arr.slice(k).concat(arr.slice(0, k));
}

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;
    }
}