579. Reverse First K Elements of Queue

EasyStackQueueStack

Given a queue represented as an array (index 0 is the front) and an integer k, reverse the order of the first k elements while keeping the remaining elements in their original order, and return the resulting queue. The input is JSON {queue, k}.

Input: JSON {queue, k}.

Output: Array — the queue after reversing the first k elements.

Examples

Example 1
Input: {"queue":[1,2,3,4,5],"k":3}
Output: [3,2,1,4,5]
Explanation: First three reversed.
Example 2
Input: {"queue":[1,2,3],"k":3}
Output: [3,2,1]
Explanation: Whole queue reversed.
Example 3
Input: {"queue":[1,2,3,4],"k":0}
Output: [1,2,3,4]
Explanation: Nothing changes.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →