Given a permutation P of size N, having values from 1 to N. the task is to find the minimum number of adjacent swaps required such that for all i in the range [1, N], P[i] does not equal i.
Input: P = [1, 4, 3, 5, 2]
Here P = [1, 4, 3, 5, 2] at index 1, 2, 3, 4, 5. As we can see, P = 1 and P = 3. Hence, we need to get rid of this invariant.
Swap 1: Swap index 1 and index 2 => [4, 1, 3, 5, 2]
Swap 2: Swap index 2 and index 3 => [4, 3, 1, 5, 2]
The final array has no i where P[i] = i. Hence, a minimum of 2 swaps is required.
Input: P = [1, 2, 4, 9, 5, 8, 7, 3, 6]
Swap 1: Swap index 1 and index 2 => [2, 1, 4, 9, 5, 8, 7, 3, 6]
Swap 2: Swap index 5 and index 6 => [2, 1, 4, 9, 8, 5, 7, 3, 6]
Swap 2: Swap index 7 and index 8 => [2, 1, 4, 9, 8, 5, 3, 7, 6]
Hence, a minimum of 3 swaps is required.
Approach: Let us consider the positions where P[i] = i be denoted by X and the other positions by O. Below are three basic observation for the question:
- If values at any two adjacent index of the permutation is of the form XO, we can simply swap the 2 indexes to get ‘OO’.
- If values at any two adjacent index of the permutation is of the form XX, we can simply swap the 2 indexes to get ‘OO’.
- If values at any two adjacent index of the permutation is of the form OX, it is simply ‘XO’ or ‘XX’ once the pointer reaches index at X.
Below are the steps:
- Iterate from 1 to N – 1 and check if P[i] = i then we simply swap(P[i], P[i + 1]), otherwise continue the process for the next adjacent pairs.
- The Corner Case for the given question is when i = N, if P[i] = i, then we swap(P[i], P[i – 1]).
Below is the implementation of above approach:
Time Complexity: O(N)
- Minimum number of given operations required to convert a permutation into an identity permutation
- Minimum number of swaps required for arranging pairs adjacent to each other
- Minimum adjacent swaps required to Sort Binary array
- Minimum steps to convert an Array into permutation of numbers from 1 to N
- Number of moves required to guess a permutation.
- Largest permutation after at most k swaps
- Sort decreasing permutation of N using triple swaps
- Minimum number of adjacent swaps for arranging similar elements together
- Minimum number of swaps required to sort an array
- Minimum number of swaps required to sort an array | Set 2
- Subarray permutation that satisfies the given condition
- Minimum number of swaps required to make the string K periodic
- Minimum number of swaps required to sort an array of first N number
- Sort permutation of N natural numbers using triple cyclic right swaps
- Find permutation of first N natural numbers that satisfies the given condition
- Permutation of a string with maximum number of characters greater than its adjacent characters
- Minimum number of prefix reversals to sort permutation of first N numbers
- Permutation of Array such that products of all adjacent elements are even
- Permutation of Array such that sum of adjacent elements are not divisible by 3
- Convert one array to another using adjacent swaps of elements
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.