Help with May Lunchtime Division 2 (2019) REMMAX

I wrote the code for the problem CodeChef: Practical coding for everyone

I cant seem to understand why I am getting an error. Please help me. My code is as follows:-

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        string s;
        cin>>s;
        int n=s.length();
        bool flag=false;
        if(n==1)
        {
            cout<<s<<endl;
            continue;
        }
        if(s[0]=='1')
        {
            flag=true;
        }
        else if(s[1]!='9')
        {
            s[0]=s[0]-1;
        }
        for(int i=1;i<n;i++)
        {
            if(flag==false)
            {
                s[i]='9';
            }
            else
            {
                if(s[i]!='0'&&i!=n-1&&s[i+1]!='9')
                {
                    s[i]=s[i]-1;
                    flag=false;
                }
            }
        }
        cout<<s<<endl;
    }
}

your code is not indented
you did not even explain what prob you are facing
How and Why one shld help you !

Still i will try
This prob has many edge cases
What i suggest is take any working prob and try all numbers from 1 to 5000 and see you get correct ans for them
No need to enter numbers manually. You can make a text file for it

hi
sorry !
my bad…I edited my question and pasted the corrected code and in that I did not notice i erased my explanation

did u understand the approach i told u ?
in that way you can debug ur code
its really difficult to spot mistakes in someone else’s code in such kind prob which has so many edge cases

Yes! Let me try that. Thanks

as @ganesh92 said you can calculate answer for n =1 to 10000 , by making a function reverse which reverses each number and returns it … and then arr[n]=max(arr[n-1],reverse(i))…arr[1]=1…now reverse (arr[n]) is your answer for n = 1 to 10000 …This will give correct answer for the subtask

1 Like