 Maximum number without a digit | Sololearn: Learn to code for FREE!

0

# Maximum number without a digit

Hello! I had to solve this problem and I got the next solution. A natural number N is read from the keyboard. Calculate the minimum number obtained by removing a single digit from the initial number. #include <iostream> using namespace std; int main() { int N; int copyOfN; int newN = 0; int index = 0; cin >> N; copyOfN = N; int i = 1; while (N != 0) { if ((N % 10) < ((N / 10) % 10)) { index = i; } N = N / 10; i++; } i = 1; while (copyOfN != 0) { if (index--) { newN = newN + ( copyOfN % 10 ) * i; i = i * 10; } copyOfN = copyOfN / 10; } cout << newN; return 0; } The question is: How can I modify the above solution to solve the following similar problem: Requirement A natural number N is read from the keyboard. Calculate the maximum number obtained by removing a single digit from the initial number. I tried to compare this time if the last digit is higher than the penultimate digit, to put an index there, but the problem is that it compares my last digit (or the first digit of the number) with the digit 0, when the number runs out of digits.

+2

I think you can do it by just by removing 1st ith digit satisfying ( ith digit ) < ( (i+1)th digit ). If no such i found, then remove last digit (I mean ones place one). Example: 100 // No such i found, so remove last 0, giving 10 51343318 // Remove 1st indexed 1 as 1<3, giving 5343318 777 // No such i found, so remove last digit 7, giving 77 Proof: if ith digit is x and (i+1)th digit is y. [satisfying x<y] Lets say answer exist for some other x' which comes after x, but ( ----xy----- ) will be less than ( ----y---x'--- ) because x<y so it is always better to remove 1st index satisfying that condition.

+1

ok, I forgot to mention that I am a beginner and that I do the problems on a basic programming platform. I still haven't gotten to work with vectors, lists, strings, functions, pointers, etc. I wanted a solution as simple as possible, as easy to understand as possible for someone who is currently practicing while loop (this is how I came to know). I will try to translate your solutions in the simplest way possible, so that I understand what I am doing here.

+1

That's how it is. You are right. thanks for the answers and I really appreciate the help of the community. I will try to translate the answer received from you into a simpler form myself, which is correct, only I am too beginner to understand it in this form. Thanks for the help

0

ok, let's take the number 329. if the last digit is higher than the penultimate digit, put an index. let's take it like this: 9> 2, put index, 2 <3, don't put index, 3> 0 put index. so index is 3. not good. any other first digit of a number (viewed from left to right) will be greater than 0. so the index will be equal to the total number of digits. which is not good. I had to stop with an index back, that is, in the case of the number 329, I had to stop at index = 1 (9> 2), in order to then meet the condition in the second while, that is index--! = 0, that is, to practically skip the number 2. how to do this? I didn't really understand your version, if you can be more explicit please or you can offer me an exact solution. thank you very much and sorry for the English.

0

In number 329, For i=0, str=3, 3 is not less than 2 For i=1, str=2, 2 is less than 9 (Since we found smallest index i satisfying it, so we need to remove it) (no need to continue loop, break from it as we wanted 1st elements satisfying str[i]<str[i+1] ) //Code: string str="329"; //take as input int n=str.size(); int idx=-1; for(int i=0;i<n-1;i++){ if(str[i]<str[i+1]){ idx=i; break; } } if(idx==-1) idx=n-1; for(int i=0;i<n;i++) if(i!=idx) cout<<str[i]; // time complexity: O(number of digits)