Largest lexicographic array with at-most K consecutive swaps

Given an array arr[], find the lexicographically largest array that can be obtained by performing at-most k consecutive swaps.

Examples :

Input : arr[] = {3, 5, 4, 1, 2}
        k = 3
Output : 5, 4, 3, 2, 1
Explanation : Array given : 3 5 4 1 2
After swap 1 : 5 3 4 1 2
After swap 2 : 5 4 3 1 2
After swap 3 : 5 4 3 2 1

Input : arr[] = {3, 5, 1, 2, 1}
        k = 3
Output : 5, 3, 2, 1, 1


Brute Force Approach : Generate all permutation of the array and then pick the one which satisfies the condition of at most K swaps. The time complexity of this approach is O(n!).

Optimized Approach : In this greedy approach, first find the largest element present in the array which is greater than(if the 1st position element is not the greatest) the 1st position and which can be placed at the 1st position with at-most K swaps. After finding that element, note its index. Then, swap elements of the array and update K value. Apply this procedure for other positions till k is non-zero or array becomes lexicographically largest.

Below is the implementation of above approach :

// C++ program to find lexicographically
// maximum value after k swaps.
#include <bits/stdc++.h>
using namespace std;

// Function which modifies the array
void KSwapMaximum(int arr[], int n, int k)
{
    for (int i = 0; i < n - 1 && k > 0; ++i) {

        // Here, indexPositionition is set where we
        // want to put the current largest integer
        int indexPosition = i;
        for (int j = i + 1; j < n; ++j) {

            // If we exceed the Max swaps
            // then break the loop
            if (k <= j - i)
                break;

            // Find the maximum value from i+1 to
            // max k or n which will replace
            // arr[indexPosition]
            if (arr[j] > arr[indexPosition])
                indexPosition = j;
        }

        // Swap the elements from Maximum indexPosition
        // we found till now to the ith index
        for (int j = indexPosition; j > i; --j)
            swap(arr[j], arr[j - 1]);

        // Updates k after swapping indexPosition-i
        // elements
        k -= indexPosition - i;
    }
}

// Driver code
int main()
{
    int arr[] = { 3, 5, 4, 1, 2 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int k = 3;

    KSwapMaximum(arr, n, k);

    // Print the final Array
    for (int i = 0; i < n; ++i)
        cout << arr[i] << " ";
}
Output:

5 4 3 1 2

Time Complexity: O(N*N)
Auxilliary Space: O(1)





Check out this Author's contributed articles.

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.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.

Recommended Posts:



4 Average Difficulty : 4/5.0
Based on 1 vote(s)