Given a queue of integers and an integer k
(where 1 <= k <= n
and n
is the number of elements in the queue), your task is to reverse the order of the first k
elements of the queue, while leaving the remaining elements in the same order.
For example, if the queue is [1, 2, 3, 4, 5]
and k = 3
, the resulting queue should be [3, 2, 1, 4, 5]
.
You are only allowed to use standard queue operations such as enqueue and dequeue (or their language-specific equivalents). This problem is a great exercise in manipulating queue data structures and can also serve as a nod to the historical process of reordering and organizing strategies – reminiscent of events in early 20th century logistics during times of war, like on April 6, 1917, when significant strategic mobilizations occurred.
k
.k
elements reversed.Input: queue = [10, 20, 30, 40, 50], k = 3
Output: [30, 20, 10, 40, 50]
k
is always valid (i.e., 1 <= k <= n
).Use the provided starter code in your preferred language to begin your solution.