Write a function solution that given a three digit integer N and Integer K, returns the maximum possible three digit value that can be obtained by performing at most K increases by 1 of any digit in N
#include <bits/stdc++.h>
using namespace std;
void Maximum(
string S, int K, string& max)
{
if (K == 0)
return;
int N = S.length();
for (int i = 0; i < N - 1; i++) {
for (int m = i + 1; m < N; m++) {
if (S[i] < S[m]) {
swap(S[i], S[m]);
if (S.compare(max) > 0)
max = S;
Maximum(S, K - 1, max);
swap(S[i], S[m]);
}
}
}
}
int main()
{
string S ;
cout<<"Enter a number: ";
cin>>S;
int K = 4;
string max = S;
Maximum(S, K, max);
cout << max << endl;
return 0;
}
Comments
Leave a comment