26. Rotate Deque By K
β GFG solution to the Rotate Deque By K problem: efficiently rotate deque left or right by k positions using optimized push/pop operations. π
π§© Problem Description
π Examples
Example 1
Input: dq = [1, 2, 3, 4, 5, 6], type = 1, k = 2
Output: [5, 6, 1, 2, 3, 4]
Explanation: The type is 1 and k is 2. So, we need to right rotate deque by 2 times.
In first right rotation we get [6, 1, 2, 3, 4, 5].
In second right rotation we get [5, 6, 1, 2, 3, 4].Example 2
Input: dq = [10, 20, 30, 40, 50], type = 2, k = 3
Output: [40, 50, 10, 20, 30]
Explanation: The type is 2 and k is 3. So, we need to left rotate deque by 3 times.
In first left rotation we get [20, 30, 40, 50, 10].
In second left rotation we get [30, 40, 50, 10, 20].
In third left rotation we get [40, 50, 10, 20, 30].π Constraints
β
My Approach
Push/Pop Operations Method
π Time and Auxiliary Space Complexity
π§βπ» Code (C++)
β Code (Java)
π Code (Python)
π§ Contribution and Support
πVisitor Count
Last updated