#include <stdio.h>
void dupes(int a[], int n)
{
int swaps = 0;
int i;
for (i = 0; i < n; i++)
while (a[a[i]] != a[i]) {
int tmp = a[i];
a[i] = a[tmp];
a[tmp] = tmp;
swaps++;
}
for (i = 0; i < n; i++)
if (a[i] != i)
printf("\n(swaps = %d)\n", swaps
); }
int main()
{
int x[] = {1, 2, 3, 1, 3, 0, 6};
dupes(x, sizeof x / sizeof x[0]);
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+Cgp2b2lkIGR1cGVzKGludCBhW10sIGludCBuKQp7CiAgICBpbnQgc3dhcHMgPSAwOwogICAgaW50IGk7CgogICAgZm9yIChpID0gMDsgaSA8IG47IGkrKykKICAgICAgICB3aGlsZSAoYVthW2ldXSAhPSBhW2ldKSB7CiAgICAgICAgICAgIGludCB0bXAgPSBhW2ldOwogICAgICAgICAgICBhW2ldID0gYVt0bXBdOwogICAgICAgICAgICBhW3RtcF0gPSB0bXA7CiAgICAgICAgICAgIHN3YXBzKys7CiAgICAgICAgfQoKICAgIGZvciAoaSA9IDA7IGkgPCBuOyBpKyspCiAgICAgICAgaWYgKGFbaV0gIT0gaSkKICAgICAgICAgICAgcHJpbnRmKCIlZCAiLCBhW2ldKTsKCiAgICBwcmludGYoIlxuKHN3YXBzID0gJWQpXG4iLCBzd2Fwcyk7Cn0KCmludCBtYWluKCkKewogICAgaW50IHhbXSA9IHsxLCAyLCAzLCAxLCAzLCAwLCA2fTsKCiAgICBkdXBlcyh4LCBzaXplb2YgeCAvIHNpemVvZiB4WzBdKTsKCiAgICByZXR1cm4gMDsKfQo=