# 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
Explanation:
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
```

## Recommended: Please solve it on “PRACTICE” first, before moving on to the solution.

A Naive approach is to one by one generate 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. Problem of this approach is that it would be difficult to implement and will definitely time out for large value of N.

An Efficient approach is to think greedily. If we visualize the problem then we will get to know that largest permutation can only be obtained if it starts with n and continues with n-1, n-2,…. So we just need to put the 1st, 2nd, 3rd, …, kth largest element to their respective position.

## C++

 `// Below is C++ code to print largest permutation ` `// after atmost K swaps ` `#include ` `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

## Java

 `// 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. `

## Python

 `# 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)) `

## C#

 `// 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. `

## PHP

 ` `

Output:

```Largest permutation after 3 swaps:
5 4 3 2 1
```

Time complexity: O(n)
Auxiliary space: O(n)

Another Method (Using Hashmap
)
Approach: We have used unordered_map in this problem as insertion and searching in unordered_map takes O(1) time on an average. The map contains the elements in the array as its keys and their indexes in the array as its values. The idea is to find the best possible index for each element in the array and make the largest possible permutation, so we are going to iterate from the (n)th element in the array and move that element to its best index at the cost of 1 swap. This operation is performed with the help of a Hashmap as we are storing the indices of the elements as the “value” in the hashmap. The best index for any element at any iteration will be N – i, where i can go from n to 1.

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

Below is the implementation of the above approach:

 `// C++ Program to find the largest permutation after ` `// at most k swaps using unordered_map. ` `#include ` `#include ` `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 "``<

Output:

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

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.