Given an integer K and a queue of integers, the task is to reverse the order of the first K elements in the queue, while keeping the remaining elements in their original relative order.
The following standard queue operations are allowed:
Complete the given function modifyQueue(), which takes a queue and an integer K as input parameters, and returns the modified queue. The driver code will handle the printing of the queue automatically.
Example 1
Input: 7 5, 1 2 3 4 5 6 7
Output: 5 4 3 2 1 6 7
Explanation: We reverse the first K = 5 elements of the queue. The first 5 elements {1, 2, 3, 4, 5} are reversed to {5, 4, 3, 2, 1}. The remaining elements {6, 7} stay in the same order. Hence, the final output is 5 4 3 2 1 6 7.
Sign in to write, run, and submit your solution against the full test suite.