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. π
The problem can be found at the following link: π Question Link
π§© Problem Description
You are given a deque dq
(double-ended queue) containing non-negative integers, along with two positive integers type
and k
. The task is to rotate the deque circularly by k
positions.
There are two types of rotation operations:
Right Rotation (Clockwise): If
type = 1
, rotate the deque to the right. This means moving the last element to the front, and repeating the process k times.Left Rotation (Anti-Clockwise): If
type = 2
, rotate the deque to the left. This means moving the first element to the back, and repeating the process k times.
π 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
$1 \le \text{dq.size()} \le 10^5$
$1 \le k \le 10^5$
$1 \le \text{type} \le 2$
β
My Approach
The optimal approach uses Direct Push/Pop Operations on the deque to achieve efficient rotation:
Push/Pop Operations Method
Handle Edge Cases:
If deque is empty or k is 0, no rotation needed.
Use modulo operation
k %= n
to handle cases where k > n.
Right Rotation (type = 1):
Move the last element to the front k times.
Use
push_front(back())
andpop_back()
operations.This simulates clockwise rotation.
Left Rotation (type = 2):
Move the first element to the back k times.
Use
push_back(front())
andpop_front()
operations.This simulates anti-clockwise rotation.
Optimization:
Deque provides O(1) operations for both ends.
No need for additional data structures or complex algorithms.
π Time and Auxiliary Space Complexity
Expected Time Complexity: O(k), where k is the number of rotations after taking modulo with deque size. Each rotation involves O(1) push/pop operations.
Expected Auxiliary Space Complexity: O(1), as we only use the existing deque without any additional space for temporary storage.
π§βπ» Code (C++)
class Solution {
public:
void rotateDeque(deque<int>& dq, int type, int k) {
int n = dq.size();
if (n == 0 || (k %= n) == 0) return;
if (type == 1) {
for (int i = 0; i < k; i++) {
dq.push_front(dq.back());
dq.pop_back();
}
} else {
for (int i = 0; i < k; i++) {
dq.push_back(dq.front());
dq.pop_front();
}
}
}
};
β Code (Java)
class Solution {
public static void rotateDeque(Deque<Integer> dq, int type, int k) {
int n = dq.size();
if (n == 0 || (k %= n) == 0) return;
if (type == 1) {
for (int i = 0; i < k; i++) {
dq.addFirst(dq.removeLast());
}
} else {
for (int i = 0; i < k; i++) {
dq.addLast(dq.removeFirst());
}
}
}
}
π Code (Python)
class Solution:
def rotateDeque(self, dq, type, k):
n = len(dq)
if n == 0 or k % n == 0: return
k %= n
if type == 1:
for _ in range(k):
dq.appendleft(dq.pop())
else:
for _ in range(k):
dq.append(dq.popleft())
π§ Contribution and Support
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: π¬ Any Questions?. Let's make this learning journey more collaborative!
β If you find this helpful, please give this repository a star! β
πVisitor Count
Last updated