Answer to Question #307281 in C++ for Kalyan

Question #307281

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
2022-03-07T16:52:29-0500
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
#include <iostream>
#include<string.h>

using namespace std;


void GetMax(string InputS, int K, string& maxval)
{
    if (K == 0)          return;
    int l = InputS.length();
    for (int i = 0; i < l - 1; i++) 
	{
        for (int m = i + 1; m < l; m++) 
		{
            if (InputS[i] < InputS[m]) 
			{
                swap(InputS[i], InputS[m]);
                if (InputS.compare(maxval) > 0)    maxval = InputS;
                GetMax(InputS, K - 1, maxval);
                swap(InputS[i], InputS[m]);
            }
        }
    }
}
int main()
{
    string Sn,max ;
    int K = 0;
    cout<<"Enter a 3-digits number (N): ";	cin>>Sn;
    cout<<"Enter K                    : ";	cin>>K;
    max = Sn;
    GetMax(Sn, K, max);
    cout <<"\n\tOutput: "<<max<<"\n";
    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