maximum possible number atmost k swaps
C++
// C++ program to find maximum
// integer possible by doing
// at-most K swap operations on
// its digits.
#include <bits/stdc++.h>
using namespace std;
// Function to find maximum
// integer possible by
// doing at-most K swap operations
// on its digits
void findMaximumNum(
string str, int k,
string& max, int ctr)
{
// return if no swaps left
if (k == 0)
return;
int n = str.length();
// Consider every digit after
// the cur position
char maxm = str[ctr];
for (int j = ctr + 1; j < n; j++) {
// Find maximum digit greater
// than at ctr among rest
if (maxm < str[j])
maxm = str[j];
}
// If maxm is not equal to k,
// decrement k
if (maxm != str[ctr])
--k;
// search this maximum among the rest
for (int j = ctr; j < n; j++) {
// If digit equals maxm swap
// the digit with current
// digit and recurse for the rest
if (str[j] == maxm) {
// swap str[ctr] with str[j]
swap(str[ctr], str[j]);
// If current num is more than
// maximum so far
if (str.compare(max) > 0)
max = str;
// recurse other swaps after cur
findMaximumNum(str, k, max, ctr + 1);
// Backtrack
swap(str[ctr], str[j]);
}
}
}
// Driver code
int main()
{
string str = "129814999";
int k = 4;
string max = str;
findMaximumNum(str, k, max, 0);
cout << max << endl;
return 0;
}
// C++ program to find maximum
// integer possible by doing
// at-most K swap operations
// on its digits.
#include <bits/stdc++.h>
using namespace std;
// Function to find maximum
// integer possible by
// doing at-most K swap
// operations on its digits
void findMaximumNum(
string str, int k, string& max)
{
// Return if no swaps left
if (k == 0)
return;
int n = str.length();
// Consider every digit
for (int i = 0; i < n - 1; i++) {
// Compare it with all digits after it
for (int j = i + 1; j < n; j++) {
// if digit at position i
// is less than digit
// at position j, swap it
// and check for maximum
// number so far and recurse
// for remaining swaps
if (str[i] < str[j]) {
// swap str[i] with str[j]
swap(str[i], str[j]);
// If current num is more
// than maximum so far
if (str.compare(max) > 0)
max = str;
// recurse of the other k - 1 swaps
findMaximumNum(str, k - 1, max);
// Backtrack
swap(str[i], str[j]);
}
}
}
}
// Driver code
int main()
{
string str = "129814999";
int k = 4;
string max = str;
findMaximumNum(str, k, max);
cout << max << endl;
return 0;
}
Also in C++:
- Title
- c++ not greater than
- Category
- C++
- Title
- how to make a 2d vector in c++
- Category
- C++
- Title
- erase in set
- Category
- C++
- Title
- c++ get type name of object
- Category
- C++
- Title
- how to measure program run time in c++
- Category
- C++
- Title
- remove element by index from vector c++
- Category
- C++
- Title
- c++ base 10 to base 2
- Category
- C++
- Title
- c++ clear stream
- Category
- C++
- Title
- object reference not set to an instance of an object c#
- Category
- C++
- Title
- c++ program for addition of two numbers using functions
- Category
- C++
- Title
- c++ get length of array
- Category
- C++
- Title
- how to turn int into string c++
- Category
- C++
- Title
- opencv compile c++
- Category
- C++
- Title
- c++ typeid get type name
- Category
- C++
- Title
- char size length c++
- Category
- C++
- Title
- caesar cipher program in c++
- Category
- C++
- Title
- number of islands leetcode code
- Category
- C++
- Title
- stack function in cpp
- Category
- C++
- Title
- c++ replace substrings
- Category
- C++
- Title
- c++ get last element in vector
- Category
- C++
- Title
- how to convert qt string to string
- Category
- C++
- Title
- c++ stack
- Category
- C++
- Title
- primeros numeors primos menores que
- Category
- C++
- Title
- c++ replace n substrings
- Category
- C++
- Title
- c++ function to find minimum element in array
- Category
- C++
- Title
- FInd the element which appears more than n/2 times C++
- Category
- C++
- Title
- linear search in c++
- Category
- C++
- Title
- pair in c++
- Category
- C++
- Title
- c++ sql
- Category
- C++
- Title
- getch c++ library
- Category
- C++
- Title
- declare vectors c++
- Category
- C++
- Title
- making random numbers in c++
- Category
- C++
- Title
- c++ declare char
- Category
- C++
- Title
- string comparison in c++
- Category
- C++
- Title
- mark occurances of elements in array cpp
- Category
- C++
- Title
- first prime numbers
- Category
- C++
- Title
- c++ std::copy to cout
- Category
- C++
- Title
- get elements of 2d array c++
- Category
- C++
- Title
- singleton c++
- Category
- C++
- Title
- two sum problem in c++
- Category
- C++
- Title
- how to include seld declared header file in c++
- Category
- C++
- Title
- initialising 2d vector
- Category
- C++
- Title
- c++ throw exception
- Category
- C++
- Title
- hashset in c++
- Category
- C++
- Title
- how to load from files C++
- Category
- C++
- Title
- how to iterate over unordered_map c++
- Category
- C++
- Title
- c++ code 2d block
- Category
- C++
- Title
- how to check sqrt of number is integer c++
- Category
- C++
- Title
- empty string c++ value
- Category
- C++
- Title
- queue c++
- Category
- C++
- Title
- c++ how to return an empty vector
- Category
- C++
- Title
- single line if c++
- Category
- C++
- Title
- how to type cast quotient of two integers to double with c++
- Category
- C++
- Title
- c++ class inheritance
- Category
- C++
- Title
- checking an int in c++
- Category
- C++
- Title
- preorder traversal c++
- Category
- C++
- Title
- multiple words C++ in same
- Category
- C++
- Title
- pop_back
- Category
- C++
- Title
- phph date
- Category
- C++
- Title
- c++ for loop
- Category
- C++
- Title
- friend function in c++
- Category
- C++
- Title
- matrix class in c++
- Category
- C++
- Title
- struct c++
- Category
- C++
- Title
- is TLE means my code is correct but taking more time to computr
- Category
- C++
- Title
- passing reference in c++
- Category
- C++
- Title
- how to read a comma delimited file into an array c++
- Category
- C++
- Title
- calculate factorial
- Category
- C++
- Title
- cin.fail()
- Category
- C++
- Title
- subarray sum in c++
- Category
- C++
- Title
- cpp loop through object
- Category
- C++
- Title
- sort function in c++
- Category
- C++
- Title
- sieve of eratosthenes c++
- Category
- C++
- Title
- never gonna give you up
- Category
- C++
- Title
- placement new c++
- Category
- C++
- Title
- how to round to nearest whole number unity
- Category
- C++
- Title
- c++ function return array
- Category
- C++
- Title
- programa para saber si un numero es primo
- Category
- C++
- Title
- vector pop back
- Category
- C++
- Title
- error: ISO C++ forbids comparison between pointer and integer [-fpermissive] if(s[i] != "b"){
- Category
- C++
- Title
- c++ try
- Category
- C++
- Title
- power c++
- Category
- C++
- Title
- & in xml
- Category
- C++
- Title
- ue4 c++ enum
- Category
- C++
- Title
- how to grab all of user input c++
- Category
- C++
- Title
- filling 2d array with 0 c++
- Category
- C++
- Title
- calling a method on an object c++
- Category
- C++
- Title
- c++ vector pop_back
- Category
- C++
- Title
- how to print a string to console in c++
- Category
- C++
- Title
- conditional operator in cpp
- Category
- C++
- Title
- how to make string get spaces c++
- Category
- C++
- Title
- pop from between string c++
- Category
- C++
- Title
- c++ map find
- Category
- C++
- Title
- basic ex of maps in c++
- Category
- C++
- Title
- c++ excel cell blank cells
- Category
- C++
- Title
- dijkstra c++ geeksforgeeks using set
- Category
- C++
- Title
- how to concatinate two strings in c++
- Category
- C++
- Title
- how to convert a string to a double c++
- Category
- C++
- Title
- what is iterator in c++?
- Category
- C++
- Title
- how to convert int to string c++
- Category
- C++
- Title
- pyqt connect
- Category
- C++