
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Largest permutation after at most k swaps in C++
In this tutorial, we are going to write a program that finds the largest permutation after at most k swaps.
Let's see the steps to solve the problem.
- Initialise the array.
- Initialise an array to store the index with size n + 1.
- Iterate over the array and store the index of each element in the position array.
- Write a loop that iterate till n and k is greater than 0.
- Store the position of n - i element in a temp variable.
- Update position of current element arr[i] with position[n - i].
- Update the position position[n - i] with current index i.
- Swap the current element arr[i] with arr[temp. Decrement k.
- Print the array elements.
Example
Let's see the code.
#include <bits/stdc++.h> using namespace std; void getLargestPermutation(int arr[], int n, int k) { int position[n + 1]; for (int i = 0; i < n; ++i) { position[arr[i]] = i; } for (int i = 0; i < n && k; ++i) { if (arr[i] == n - i) { continue; } int temp = position[n - i]; position[arr[i]] = position[n - i]; position[n - i] = i; swap(arr[temp], arr[i]); --k; } } int main() { int arr[] = { 5, 3, 2, 6, 7, 1, 4 }; int n = 7, k = 3; getLargestPermutation(arr, n, k); for (int i = 0; i < n; ++i) { cout << arr[i]; } cout << endl; return 0; }
Output
If you run the above code, then you will get the following result.
7653214
Conclusion
If you have any queries in the tutorial, mention them in the comment section.
- Related Articles
- Program to find number of sequences after adjacent k swaps and at most k swaps in Python
- Find Maximum number possible by doing at-most K swaps in C++
- Program to find minimum possible integer after at most k adjacent swaps on digits in Python
- Largest number less than X having at most K set bits in C++
- Maximum number of fixed points using at most 1 swaps in C++
- Largest sum subarray with at-least k numbers in C++
- Find the k largest numbers after deleting the given elements in C++
- Longest Substring with At Most K Distinct Characters in C++
- Form the largest number using at most one swap operation C++
- Maximum Subarray Sum after inverting at most two elements in C++
- Count substrings with each character occurring at most k times in C++
- Program to find min length of run-length encoding after removing at most k characters in Python
- Program to find most occurring number after k increments in python
- Program to check whether palindrome can be formed after deleting at most k characters or not in python
- Maximize the maximum subarray sum after removing at most one element in C++

Advertisements