#K35507. Minimum Operations to Bring Element to Front
Minimum Operations to Bring Element to Front
Minimum Operations to Bring Element to Front
You are given an array of distinct integers and a target integer X. In one operation, you can remove the first element of the array and append it to the end. Your task is to determine the minimum number of such operations required so that the element X becomes the first element in the array.
In other words, if X is initially at index i (0-indexed), then it will become the first element after i operations. For example, if X is already at the front, then no operations are needed.
Note: It is guaranteed that X exists in the array.
The answer can be mathematically expressed as:
inputFormat
The first line contains an integer T
denoting the number of test cases. Each test case consists of two lines. The first line of each test case contains two integers N
and X
, where N
is the number of elements in the array and X
is the target integer. The second line contains N
space-separated integers representing the array.
outputFormat
For each test case, output a single integer — the minimum number of operations required to bring X
to the front of the array. Print each answer on a new line.
5
5 3
1 2 3 4 5
7 4
7 2 6 1 4 5 3
4 1
1 2 3 4
3 10
5 10 15
6 2
8 4 6 2 1 3
2
4
0
1
3
</p>