Largest permutation after at most k swaps

Given a permutation of first n natural numbers as array and an integer k. Print the lexicographically largest permutation after at most k swaps
Examples:

Input: arr[] = {4, 5, 2, 1, 3}
       k = 3
Output: 5 4 3 2 1
Swap 1st and 2nd elements: 5 4 2 1 3 
Swap 3rd and 5th elements: 5 4 3 1 2 
Swap 4th and 5th elements: 5 4 3 2 1 

Input: arr[] = {2, 1, 3}
       k = 1
Output: 3 1 2
Swap 1st and 3re elements: 3 1 2 

Naive approach: The idea is to generate one by one permutation in lexicographically decreasing order. Compare every generated permutation with original array and count the number of swaps required to convert. If count is less than or equal to k, print this permutation. The problem of this approach is that it would be difficult to implement and will definitely time out for the large value of N.

Algorithm:

  1. To find the minimimum swaps to convert one array to another read this article.
  2. Copy the original array and sort that array in decreasing order. So the sorted array is the largest permutation of the original array.
  3. Now generate all permutation in lexicographically decreasing order. Previous permutation is calculated using prev_permutation() function.
  4. Find the minimum steps required to convert the new array (permutation in decreasing order) to original array, if the count is less than or equal to k. Then print the array and break.
filter_none

edit
close

play_arrow

link
brightness_4
code

#include <bits/stdc++.h>
using namespace std;
  
// Function returns the minimum number
// of swaps required to sort the array
// This method is taken from below post
// https:// www.geeksforgeeks.org/
// minimum-number-swaps-required-sort-array/
int minSwapsToSort(int arr[], int n)
{
    // Create an array of pairs where first
    // element is array element and second
    // element is position of first element
    pair<int, int> arrPos[n];
    for (int i = 0; i < n; i++) {
        arrPos[i].first = arr[i];
        arrPos[i].second = i;
    }
  
    // Sort the array by array element
    // values to get right position of
    // every element as second
    // element of pair.
    sort(arrPos, arrPos + n);
  
    // To keep track of visited elements.
    // Initialize all elements as not
    // visited or false.
    vector<bool> vis(n, false);
  
    // Initialize result
    int ans = 0;
  
    // Traverse array elements
    for (int i = 0; i < n; i++) {
        // Already swapped and corrected or
        // already present at correct pos
        if (vis[i] || arrPos[i].second == i)
            continue;
  
        // Find out the number of  node in
        // this cycle and add in ans
        int cycle_size = 0;
        int j = i;
        while (!vis[j]) {
            vis[j] = 1;
  
            // move to next node
            j = arrPos[j].second;
            cycle_size++;
        }
  
        // Update answer by adding current
        // cycle.
        ans += (cycle_size - 1);
    }
  
    // Return result
    return ans;
}
  
// method returns minimum number of
// swap to make array B same as array A
int minSwapToMakeArraySame(
    int a[], int b[], int n)
{
    // Map to store position of elements
    // in array B we basically store
    // element to index mapping.
    map<int, int> mp;
    for (int i = 0; i < n; i++)
        mp[b[i]] = i;
  
    // now we're storing position of array
    // A elements in array B.
    for (int i = 0; i < n; i++)
        b[i] = mp[a[i]];
  
    /* We can uncomment this section to 
      print modified b array 
    for (int i = 0; i < N; i++) 
        cout << b[i] << " "; 
    cout << endl; */
  
    // Returing minimum swap for sorting
    // in modified array B as final answer
    return minSwapsToSort(b, n);
}
  
// Function to calculate largest
// permutation after atmost K swaps
void KswapPermutation(
    int arr[], int n, int k)
{
    int a[n];
  
    // copy the array
    for (int i = 0; i < n; i++)
        a[i] = arr[i];
  
    // Sort the array in descending order
    sort(arr, arr + n, greater<int>());
  
    // generate permutation in lexicographically
    // decreasing order.
    do {
        // copy the array
        int a1[n], b1[n];
        for (int i = 0; i < n; i++) {
            a1[i] = arr[i];
            b1[i] = a[i];
        }
  
        // Check if it can be made same in k steps
        if (
            minSwapToMakeArraySame(
                a1, b1, n)
            <= k) {
            // Print the array
            for (int i = 0; i < n; i++)
                cout << arr[i] << " ";
            break;
        }
  
        // move to previous permutation
    } while (prev_permutation(arr, arr + n));
}
  
int main()
{
    int arr[] = { 4, 5, 2, 1, 3 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int k = 3;
  
    cout << "Largest permutation after "
         << k << " swaps:\n";
  
    KswapPermutation(arr, n, k);
    return 0;
}

chevron_right


Output:

Largest permutation after 3 swaps:
5 4 3 2 1 

Complexity Analysis:



  • Time Complexity: O(N!).
    To generate all permutation O(N!) time complexity is required.
  • Space Complexity: O(n).
    to store the new array O(n) space is required.

Efficient approach: This is a greedy approach. The largest permutation is found when the largest elements are at the front of the array, i.e. the largest elements are sorted in decreasing order. There are at most k swaps so put the 1st, 2nd, 3rd, …, kth largest element at their respective position.

Note: If the number of swaps allowed is equal to the size of the array, then there is no need to iterate over the whole array. The answer will simply be the reverse sorted array.

Algorithm:

  1. Create a HashMap or an array of length n to store element-index pair or map element to its index.
  2. Now run a loop k times.
  3. In each iteration swap the ith element with the element n – i. where i is the index or count of the loop. Also swap their position, i.e. update the hashmap or array. So in this step the largest element in remaining element is swapped to the front.
  4. Print the output array.

Implementation 1: This uses simple arrays to arrive at the solution.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// Below is C++ code to print largest
// permutation after at most K swaps
#include <bits/stdc++.h>
using namespace std;
  
// Function to calculate largest
// permutation after atmost K swaps
void KswapPermutation(
    int arr[], int n, int k)
{
    // Auxiliary dictionary of
    // storing the position of elements
    int pos[n + 1];
  
    for (int i = 0; i < n; ++i)
        pos[arr[i]] = i;
  
    for (int i = 0; i < n && k; ++i) {
        // If element is already i'th largest,
        // then no need to swap
        if (arr[i] == n - i)
            continue;
  
        // Find position of i'th
        // largest value, n-i
        int temp = pos[n - i];
  
        // Swap the elements position
        pos[arr[i]] = pos[n - i];
        pos[n - i] = i;
  
        // Swap the ith largest value with the
        // current value at ith place
        swap(arr[temp], arr[i]);
  
        // decrement number of swaps
        --k;
    }
}
  
// Driver code
int main()
{
    int arr[] = { 4, 5, 2, 1, 3 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int k = 3;
  
    KswapPermutation(arr, n, k);
    cout << "Largest permutation after "
         << k << " swaps:n";
    for (int i = 0; i < n; ++i)
        printf("%d ", arr[i]);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Below is Java code to print
// largest permutation after
// atmost K swaps
class GFG {
  
    // Function to calculate largest
    // permutation after atmost K swaps
    static void KswapPermutation(
        int arr[], int n, int k)
    {
  
        // Auxiliary dictionary of storing
        // the position of elements
        int pos[] = new int[n + 1];
  
        for (int i = 0; i < n; ++i)
            pos[arr[i]] = i;
  
        for (int i = 0; i < n && k > 0; ++i) {
  
            // If element is already i'th
            // largest, then no need to swap
            if (arr[i] == n - i)
                continue;
  
            // Find position of i'th largest
            // value, n-i
            int temp = pos[n - i];
  
            // Swap the elements position
            pos[arr[i]] = pos[n - i];
            pos[n - i] = i;
  
            // Swap the ith largest value with the
            // current value at ith place
            int tmp1 = arr[temp];
            arr[temp] = arr[i];
            arr[i] = tmp1;
  
            // decrement number of swaps
            --k;
        }
    }
  
    // Driver method
    public static void main(String[] args)
    {
  
        int arr[] = { 4, 5, 2, 1, 3 };
        int n = arr.length;
        int k = 3;
  
        KswapPermutation(arr, n, k);
  
        System.out.print(
            "Largest permutation "
            + "after " + k + " swaps:\n");
  
        for (int i = 0; i < n; ++i)
            System.out.print(arr[i] + " ");
    }
}
  
// This code is contributed by Anant Agarwal.

chevron_right


Python

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python code to print largest permutation after K swaps
  
def KswapPermutation(arr, n, k):
  
    # Auxiliary array of storing the position of elements
    pos = {}
    for i in range(n):
        pos[arr[i]] = i
  
    for i in range(n):
  
        # If K is exhausted then break the loop
        if k == 0:
            break
  
        # If element is already largest then no need to swap
        if (arr[i] == n-i):
            continue
  
        # Find position of i'th largest value, n-i
        temp = pos[n-i]
  
        # Swap the elements position
        pos[arr[i]] = pos[n-i]
        pos[n-i] = i
  
        # Swap the ith largest value with the value at 
        # ith place
        arr[temp], arr[i] = arr[i], arr[temp]
  
        # Decrement K after swap
        k = k-1
  
# Driver code
arr = [4, 5, 2, 1, 3]
n = len(arr)
k = 3
KswapPermutation(arr, N, K)
  
# Print the answer
print "Largest permutation after", K, "swaps: "
print " ".join(map(str, arr))

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// Below is C# code to print largest
// permutation after atmost K swaps.
using System;
  
class GFG {
  
    // Function to calculate largest
    // permutation after atmost K
    // swaps
    static void KswapPermutation(int[] arr,
                                 int n, int k)
    {
  
        // Auxiliary dictionary of storing
        // the position of elements
        int[] pos = new int[n + 1];
  
        for (int i = 0; i < n; ++i)
            pos[arr[i]] = i;
  
        for (int i = 0; i < n && k > 0; ++i) {
  
            // If element is already i'th
            // largest, then no need to swap
            if (arr[i] == n - i)
                continue;
  
            // Find position of i'th largest
            // value, n-i
            int temp = pos[n - i];
  
            // Swap the elements position
            pos[arr[i]] = pos[n - i];
            pos[n - i] = i;
  
            // Swap the ith largest value with
            // the current value at ith place
            int tmp1 = arr[temp];
            arr[temp] = arr[i];
            arr[i] = tmp1;
  
            // decrement number of swaps
            --k;
        }
    }
  
    // Driver method
    public static void Main()
    {
  
        int[] arr = { 4, 5, 2, 1, 3 };
        int n = arr.Length;
        int k = 3;
  
        KswapPermutation(arr, n, k);
  
        Console.Write("Largest permutation "
                      + "after " + k + " swaps:\n");
  
        for (int i = 0; i < n; ++i)
            Console.Write(arr[i] + " ");
    }
}
  
// This code is contributed nitin mittal.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP code to print largest permutation
// after atmost K swaps
  
// Function to calculate largest 
// permutation after atmost K swaps
function KswapPermutation(&$arr, $n, $k)
{
  
    for ($i = 0; $i < $n; ++$i)
        $pos[$arr[$i]] = $i;
  
    for ($i = 0; $i < $n && $k; ++$i)
    {
        // If element is already i'th largest,
        // then no need to swap
        if ($arr[$i] == $n - $i)
            continue;
  
        // Find position of i'th largest 
        // value, n-i
        $temp = $pos[$n - $i];
  
        // Swap the elements position
        $pos[$arr[$i]] = $pos[$n - $i];
        $pos[$n - $i] = $i;
  
        // Swap the ith largest value with the
        // current value at ith place
        $t = $arr[$temp];
        $arr[$temp] = $arr[$i];
        $arr[$i] = $t;
          
        // decrement number of swaps
        --$k;
    }
}
  
// Driver code
$arr = array(4, 5, 2, 1, 3);
$n = sizeof($arr);
$k = 3;
  
KswapPermutation($arr, $n, $k);
echo ("Largest permutation after ");
echo ($k);
echo (" swaps:\n");
for ($i = 0; $i < $n; ++$i)
{
    echo ($arr[$i] );
    echo (" ");
  
// This code is contributed
// by Shivi_Aggarwal
?>

chevron_right



Output:

Largest permutation after 3 swaps:
5 4 3 2 1 

Implementation 2: This uses a hashmap to arrive at the solution.

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ Program to find the
// largest permutation after
// at most k swaps using unordered_map.
#include <bits/stdc++.h>
#include <unordered_map>
using namespace std;
  
// Function to find the largest
// permutation after k swaps
void bestpermutation(
    int arr[], int k, int n)
{
    // Storing the elements and
    // their index in map
    unordered_map<int, int> h;
    for (int i = 0; i < n; i++) {
        h.insert(make_pair(arr[i], i));
    }
  
    // If number of swaps allowed
    // are equal to number of elements
    // then the resulting permutation
    // will be descending order of
    // given permutation.
    if (n <= k) {
        sort(arr, arr + n, greater<int>());
    }
    else {
  
        for (int j = n; j >= 1; j--) {
            if (k > 0) {
  
                int initial_index = h[j];
                int best_index = n - j;
  
                // if j is not at it's best index
                if (initial_index != best_index) {
  
                    h[j] = best_index;
  
                    // Change the index of the element
                    // which was at position 0. Swap
                    // the element basically.
                    int element = arr[best_index];
                    h[element] = initial_index;
                    swap(
                        arr[best_index],
                        arr[initial_index]);
  
                    // decrement number of swaps
                    k--;
                }
            }
        }
    }
}
  
// Driver code
int main()
{
    int arr[] = { 3, 1, 4, 2, 5 };
  
    // K is the number of swaps
    int k = 10;
  
    // n is the size of the array
    int n = sizeof(arr) / sizeof(int);
  
    // Function calling
    bestpermutation(arr, k, n);
  
    cout << "Largest possible permutation after "
         << k << " swaps is ";
    for (int i = 0; i < n; i++)
        cout << arr[i] << " ";
  
    return 0;
}
// This method is contributed by Kishan Mishra.

chevron_right


Output:

Largest possible permutation after 3 swaps is 5 4 3 2 1

Complexity Analysis:

  • Time Complexity: O(N).
    Only one traversal of the array is required.
  • Space Complexity: O(n).
    To store the new array O(n) space is required.

This article is contributed by Shubham Bansal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up