#K90552. Minimum Adjacent Swaps to Move Target to End
Minimum Adjacent Swaps to Move Target to End
Minimum Adjacent Swaps to Move Target to End
Given a sorted list of unique integers and a target integer present in the list, determine the minimum number of adjacent swaps required to move the target to the end of the list.
In one swap operation, you can exchange an element with its immediate neighbor. Since the list is already sorted, the optimal solution is to repeatedly swap the target element until it occupies the last position.
The required number of swaps can be computed by the formula:
\(\text{swaps} = n - i - 1\)
where \(n\) is the number of elements in the list and \(i\) is the index (0-indexed) of the target element.
inputFormat
The input is provided via stdin and consists of the following three lines:
- The first line contains an integer n, representing the number of elements in the list.
- The second line contains n space-separated integers, which form a sorted list of unique numbers.
- The third line contains an integer representing the target element that needs to be moved to the end.
outputFormat
Output via stdout a single integer: the minimum number of adjacent swaps required to move the target element to the end of the list.
## sample5
1 3 5 7 9
5
2