Answer to Question #256378 in C for Appu

Question #256378

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



1
Expert's answer
2021-10-26T01:03:28-0400
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS