#C5454. Minimum Swaps to Make an Array Good
Minimum Swaps to Make an Array Good
Minimum Swaps to Make an Array Good
You are given an array of length (n) containing a permutation of the integers from (1) to (n). A "good array" is defined as an array in which each element is equal to its index position when using 1-based indexing, i.e., (a_i = i) for all (1 \leq i \leq n). Your task is to determine the minimum number of swaps required to transform the given array into a good array.
A swap involves exchanging the positions of any two elements in the array.
Input Format: The first line contains a single integer (n) representing the size of the array. The second line contains (n) space-separated integers representing the array elements.
Output Format: Print a single integer denoting the minimum number of swaps required.
Note: The solution must read input from standard input (stdin) and output the answer to standard output (stdout).
inputFormat
The first line contains an integer (n) (the size of the array). The second line contains (n) space-separated integers representing a permutation of (1) to (n).
outputFormat
Output a single integer which is the minimum number of swaps required to convert the array into a good array.## sample
5
3 5 4 1 2
3