#C8059. Minimum Swaps to Unique Sequence
Minimum Swaps to Unique Sequence
Minimum Swaps to Unique Sequence
You are given a sequence of integers. Your task is to determine the minimum number of swaps required to transform the given sequence into one in which every integer appears exactly once, while preserving the original relative order. In other words, you should remove duplicates from the sequence by swapping duplicate elements out of their original positions.
The problem can be mathematically formulated as follows:
Given a sequence of length \(N\) and a set of unique integers \(U\) that appear in the sequence, the minimum number of swaps required is:
$$\text{minimum swaps} = N - |U|$$
Note: Although the operation is described as swapping, the process corresponds to removing the extra occurrences such that each integer appears only once.
inputFormat
The first line contains an integer \(T\), the number of test cases. Each test case is given in the following format:
- A line containing an integer \(N\) which represents the length of the sequence.
- A line with \(N\) space-separated integers representing the sequence.
All input is read from standard input (stdin).
outputFormat
For each test case, output a single line containing an integer — the minimum number of swaps needed to transform the sequence. All output should be printed to standard output (stdout).
## sample4
5
2 3 3 2 1
7
4 5 4 4 3 5 4
4
1 2 3 4
6
1 1 1 1 1 1
2
4
0
5
</p>