Minimum no of swaps to group similar items together.

Revision en1, by v0rtex, 2021-10-24 23:29:44

We are given an array of integers 1 <= a[i] <= 20, of size N (1<= N <= 10 ^ 5) and we have to group similar elements together. In one operation we can swap two adjacent elements. Find the minimum no of swaps.

Input — 1 2 1 2 1 Output — 3

Input — 1 3 3 2 Output — 0

Input — 1 3 2 1 2 3 Output — 4

Explanation of test case 3 — swap a[2] with a[3] swap a[4] with a[3] swap a[4] with a[5] swap a[2] with a[3] The array becomes — { 1, 1, 2, 2, 3, 3}

Tags minimum number of swaps, group, arrays, #integers

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en1 English v0rtex 2021-10-24 23:29:44 601 Initial revision (published)