#K58512. Minimum Swaps to Sort an Array

    ID: 30659 Type: Default 1000ms 256MiB

Minimum Swaps to Sort an Array

Minimum Swaps to Sort an Array

You are given an array of n unique integers. Your task is to find the minimum number of swap operations required to sort the array in ascending order, such that the element at index i becomes i+1 for all i (0-indexed).

A swap operation exchanges the positions of two elements in the array. To solve this problem, you may use a strategy where you map the values to their indices and swap elements that are not in their correct positions until the array is fully sorted.

The following is an example:

Input:
5
4 3 1 2 5

Output: 3

</p>

In the example, 3 swaps are required to sort the array.

inputFormat

The input is given via stdin with the following structure:

  • The first line contains an integer n, representing the number of elements in the array.
  • The second line contains n space-separated unique integers.

outputFormat

The output should be printed to stdout as a single integer representing the minimum number of swaps required to sort the array.

## sample
5
4 3 1 2 5
3