#C501. Minimum Operations to Bring Target to Front
Minimum Operations to Bring Target to Front
Minimum Operations to Bring Target to Front
You are given a queue of integers and a target integer. Your task is to determine the minimum number of operations required to bring the target integer to the front of the queue. In one operation, you can either perform a left rotation (move the front element to the back) or a right rotation (move the back element to the front).
If the target integer does not exist in the queue, output -1
.
The answer is determined by calculating the position i (0-indexed) of the target in the queue and then taking the minimum of i (operations by left rotations) and n - i (operations by right rotations), where n is the length of the queue.
For example, consider the queue [1, 2, 3, 4, 5] with target 3. Since 3 is at index 2, the minimum number of operations needed is min(2, 5 - 2) = min(2, 3) = 2.
inputFormat
Input is read from standard input (stdin). The input consists of three lines:
- The first line contains an integer n, denoting the number of elements in the queue.
- The second line contains n space-separated integers representing the elements of the queue.
- The third line contains a single integer, the target number.
outputFormat
Output the minimum number of operations required to bring the target to the front of the queue. If the target is not present in the queue, output -1. The result should be printed to standard output (stdout).## sample
5
1 2 3 4 5
3
2