# Permutation change: Minimum operations to accomplish permutation

Offered a variety arr[] of length n, the job is to discover the minimum variety of operations needed to make a permutation of integers 1 to n where you are permitted to choose any aspect arr[i] and change it with arr[i] % x (x is any favorable integer). If it is not possible to make a permutation return -1.

Examples:

Input: n = 5, arr[] = {2, 3, 4, 4, 5}
Output: 1
Description: One method to make the provided variety a permutation of integers 1 to 5 is to carry out the following operations:

• Select arr 3 = 4 and change it with arr 3 % 3 = 1.

After these operations, the variety ends up being [2, 3, 4, 1, 5], which is a permutation of integers 1 to 5. For that reason, the minimum variety of operations needed is 1.

Method: This can be resolved with the following concept:

To make the provided variety a permutation of integers 1 to n, we require to guarantee that all the components in the variety 1 to n exist precisely as soon as in the variety arr. We can accomplish this by carrying out the operation on the components in the variety that are either higher than n or duplicated. By carrying out the operation on any aspect arr i, we can transform it to any favorable number less than flooring(( arr i -1)/ 2). Given that the optimum worth of a i% x for any approximate x can be flooring (( arri-1)/ 2), we can utilize this truth to guarantee that all the components in the variety 1 to n exist precisely as soon as in the variety arr.

Below are the actions included:

• Very first produces a set including integers from 1 to n and a vector to shop components that are not part of the permutation.
• It then repeats over the input variety and gets rid of the components from the set if they exist, and includes them to the vector otherwise.
• The vector is then arranged in coming down order.
• It then repeats over the components in the vector, and for each aspect, it discovers the biggest integer s in the set and each integer in the vector state z such that (z-1)/ 2 < < s. The factor for this condition is that whenever we use operation z% x where x is any favorable integer the outcome will be constantly less than (z-1)/ 2. If such an integer exists, it eliminates it from the set. If not, it sets a flag and exits the loop.
• Lastly, it outputs the size of the vector if the flag is not set, or -1 otherwise.

Below is the execution of the above technique:

## C++

```// C++ code for the above technique:. #include < bits/stdc++. h>> . utilizing namespace sexually transmitted disease;.
. void checkPermutation( intn
, int arr[] )
. {. .// Set to save components from 1 to n . set << int > st; .
for( inti
= 1;
i
v;
. . for (int i= 0;
i < n;
i + +) {. .
// Aspect is not present .// in the set.
.
if( st.find( arr [i])== st.end()) {.
. v.push _ back( arr[i]); .}
.
. else {
. st.erase( arr(
*
)); .} .
} . .// Arranging aspect in decending order .
// in vector v .
sort( v.begin(), v.end ());. reverse( v.begin(), v.end());
. .
// If it is difficult to transform .
// variety in permutation of 1 to n .// < flag will hold true .
bool flag = incorrect;.
for( car z: v) {
.
car it= st.end();
. it--;< .
<< int s=
* it;
. if(
( z - 1) <	```