# Lexicographically largest string using at most K swaps at same parity indices

Given string **S** and a positive integer **K**, the task is to find lexicographically the largest possible string using at most **K** swaps with the condition that the indices that are swapped must be either both odd or both even.

**Examples: **

Input:S = “ancqz”, K = 2Output: “zqcna“Explanation:In one swap, we can swap characters ‘n’ and ‘q’ as they both are at even indices (2 and 4 assuming 1-based indexing). The string becomes “aqcnz”. In the second swap we can swap characters ‘a’ and ‘z’ as both have odd indices. The final string “zqcna” is the largest lexicographically possible using 2 swap operations.Note: We cannot swap for instance ‘a’ and ‘n’ or ‘n’ and ‘z’ as one of them would be at an odd index while the other at even index.

Input:S = “geeksforgeeks”, K = 3Output: “sreksfoegeekg“

**Naive approach: **The naive approach is trivial. Use the greedy algorithm to make the current index maximum starting from left by picking the maximum possible character that is to the right of the current index and is also with the same parity of index i.e. (odd if the current index is odd and even if the current index is even). Repeat the same procedure atmost **K **times. The time complexity of the approach will be **O(N ^{2})**.

**Efficient Approach: **The above approach can be improved using a priority queue. Follow the steps below to solve the problem:

- Create two priority queue one for odd index characters and another for even index characters.
- Iterate over characters in the string, if the even index character comes then search the index which is greater than the current index and character bigger than the current character in the priority queue which is holding even characters. If there is any, swap the two characters push the current character and the index that we found in the priority queue.
- The same procedure is to be followed when the odd character comes.
- If
**K**becomes**0**, terminate the loop. - The resultant string will be the answer.

Below is the implementation of the above approach:

## C++

`// C++ program of the above approach` `#include <bits/stdc++.h>` `using` `namespace` `std;` ` ` `// Function which returns` `// the largest possible string` `string lexicographicallyLargest(string S, ` `int` `K)` `{` ` ` `// Finding length of the string` ` ` `int` `n = S.length();` ` ` ` ` `// Creating two priority queues of pairs` ` ` `// for odd and even indices separately` ` ` `priority_queue<pair<` `char` `, ` `int` `> > pqOdd, pqEven;` ` ` ` ` `// Storing all possible even` ` ` `// indexed values as pairs` ` ` `for` `(` `int` `i = 2; i < n; i = i + 2) {` ` ` `// Stores pair as {character, index}` ` ` `pqEven.push(make_pair(S[i], i));` ` ` `}` ` ` ` ` `// Storing all possible odd indexed` ` ` `// values as pairs` ` ` `for` `(` `int` `i = 3; i < n; i = i + 2) {` ` ` `// Stores pair as {character, index}` ` ` `pqOdd.push(make_pair(S[i], i));` ` ` `}` ` ` ` ` `for` `(` `int` `i = 0; i < n; i++) {` ` ` `// For even indices` ` ` `if` `(i % 2 == 0) {` ` ` ` ` `// Removing pairs which` ` ` `// cannot be used further` ` ` `while` `(!pqEven.empty()` ` ` `and pqEven.top().second <= i)` ` ` `pqEven.pop();` ` ` ` ` `// If a pair is found whose index comes after` ` ` `// the current index and its character is` ` ` `// greater than the current character` ` ` `if` `(!pqEven.empty()` ` ` `and pqEven.top().first > S[i]) {` ` ` ` ` `// Swap the current index with index of` ` ` `// maximum found character next to it` ` ` `swap(S[i], S[pqEven.top().second]);` ` ` ` ` `int` `idx = pqEven.top().second;` ` ` `pqEven.pop();` ` ` `// Push the updated character at idx index` ` ` `pqEven.push({ S[idx], idx });` ` ` `K--;` ` ` `}` ` ` `}` ` ` `// For odd indices` ` ` `else` `{` ` ` `// Removing pairs which cannot` ` ` `// be used further` ` ` ` ` `while` `(!pqOdd.empty()` ` ` `and pqOdd.top().second <= i)` ` ` `pqOdd.pop();` ` ` ` ` `// If a pair is found whose index comes after` ` ` `// the current index and its character is` ` ` `// greater than the current character` ` ` ` ` `if` `(!pqOdd.empty()` ` ` `and pqOdd.top().first > S[i]) {` ` ` ` ` `// Swap the current index with index of` ` ` `// maximum found character next to it` ` ` `swap(S[i], S[pqOdd.top().second]);` ` ` `int` `idx = pqOdd.top().second;` ` ` `pqOdd.pop();` ` ` ` ` `// Push the updated character at idx index` ` ` `pqOdd.push({ S[idx], idx });` ` ` `K--;` ` ` `}` ` ` `}` ` ` `// Breaking out of the loop if K=0` ` ` `if` `(K == 0)` ` ` `break` `;` ` ` `}` ` ` ` ` `return` `S;` `}` ` ` `// Driver Code` `int` `main()` `{` ` ` `// Input` ` ` `string S = ` `"geeksforgeeks"` `;` ` ` `int` `K = 2;` ` ` ` ` `// Function Call` ` ` `cout << lexicographicallyLargest(S, K);` ` ` `return` `0;` `}` |

**Output:**

sreksfoegeekg

**Time Complexity: **O(NlogN)**Auxiliary Space:*** *O(N)

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. To complete your preparation from learning a language to DS Algo and many more, please refer **Complete Interview Preparation Course****.**

In case you wish to attend **live classes **with experts, please refer **DSA Live Classes for Working Professionals **and **Competitive Programming Live for Students**.