Suit with a tie
Irynka hosted a white slippers-themed party with a budget of 200 pounds. 2n guests attended, forming n pairs. After the party, she wants to arrange them for a group photo. The 2n guests are currently standing in a row, but Irynka is not satisfied with their arrangement. She wants each pair to stand next to each other to make the photo look more appealing.Your task is to determine the minimum number of adjacent swaps needed to ensure that each pair stands together in the row.
Input
The first line contains a single integer n (1 ≤ n ≤ 1000) – the number of pairs of people.
The second line contains 2n integers a[1]
, a[2]
, ..., a[2n]
. For each i that satisfies the condition 1 ≤ i ≤ n, the integer i appears exactly twice in this list. If a[j]
= a[k]
= i, then the people at positions j and k form a pair.
Output
Output a single integer – the minimum number of swaps required.