#C5118. Queue Manipulation: Reverse the First K Elements

    ID: 48732 Type: Default 1000ms 256MiB

Queue Manipulation: Reverse the First K Elements

Queue Manipulation: Reverse the First K Elements

You are given a sequence of integers which are to be enqueued into an initially empty queue. After the enqueuing process, you are provided with an integer \(K\). Your task is to reverse the order of the first \(K\) elements of the queue.

If \(K \le 0\) or \(K\) is greater than the number of elements in the queue, the queue should remain unchanged. Otherwise, reverse the first \(K\) elements while keeping the order of the remaining elements intact.

Example:

Input:
5
10 20 30 40 50
3

Output: 30 20 10 40 50

</p>

Hint: You may use a stack to reverse the first \(K\) elements and then perform a rotation for the rest of the queue.

inputFormat

The input consists of three lines:

  • The first line contains an integer \(n\) representing the number of elements to enqueue.
  • The second line contains \(n\) space-separated integers representing the elements to be enqueued.
  • The third line contains an integer \(K\) indicating the number of elements from the front of the queue that should be reversed.

outputFormat

Output the elements of the modified queue in a single line separated by a single space.

## sample
5
10 20 30 40 50
3
30 20 10 40 50