- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Find Maximum number possible by doing at-most K swaps in C++

In this problem, we are given two integer values n and k. Our task is to *find the Maximum number possible by doing at-most K swaps. *

**Problem description: **Here, we need to calculate the number which is maximum and created after swapping at-most k digits of the number.

**Let’s take an example to understand the problem, **

**Input: **n = 538 k = 1

**Output: **835

**Explanation: **

We will swap 8 and 5.

## Solution Approach

To solve the problem, we need to swap digits of the number k times and check if the number from is maximum. ** **

We need to find the maximum digit of the number and then swap the element at first index. And so on for the first k index of the numbers.

## Program to illustrate the working of our solution

## Example

#include <bits/stdc++.h> using namespace std; void calcMaxNumAfterSwap(string number, int k, string& maxString, int n){ if (k == 0) return; for (int i = 0; i < n - 1; i++) { for (int j = i + 1; j < n; j++) { if (number[i] < number[j]) { swap(number[i], number[j]); if (number.compare(maxString) > 0) maxString = number; calcMaxNumAfterSwap(number, k - 1, maxString, n); swap(number[i], number[j]); } } } } int main(){ string str = "15263"; int k = 3; int size = str.length(); string maxString = str; calcMaxNumAfterSwap(str, k, maxString, size); cout<<"The maximum number created after "<<k<<" swaps is "<<maxString; return 0; }

## Output

The maximum number created after 3 swaps is 65321

- Related Questions & Answers
- Program to find number of sequences after adjacent k swaps and at most k swaps in Python
- Program to find minimum possible integer after at most k adjacent swaps on digits in Python
- Largest permutation after at most k swaps in C++
- Maximum number of fixed points using at most 1 swaps in C++
- Program to find maximum sum by performing at most k negate operations in Python
- Maximum subarray sum by flipping signs of at most K array elements in C++
- Find the maximum cost of an array of pairs choosing at most K pairs in C++
- C++ program to find maximum how many chocolates we can buy with at most k rupees
- Finding the maximum number using at most one swap in JavaScript
- Maximum profit by buying and selling a share at most twice
- Program to find how many ways we can climb stairs (maximum steps at most k times) in Python
- Smallest number formed by shuffling one digit at most in JavaScript
- Program to find maximum number by adding 5 at any place in Python
- Largest number less than X having at most K set bits in C++
- Program to find sum of rectangle whose sum at most k in Python

Advertisements