Can anyone help me with Coding Arena from TCS Codevita 2017 Contest?

Hello friends can someone tell me how to approach this problem from TCS Codevita 2017 ?

@vijju123 @taran_1407 @vivek_1998299 @meooow @john_smith_3 @aryanc403 @vbt_95

alt text

2 Likes

First check if the given number is valid then print itself. Otherwise find the first digit at which the condition of non-decreasing is being violated.
Ex: 549, condition is violated at 4.
Change all digit from that digit to end to 9 and decrease previous one by 1. So from 549 we get 499.

2 Likes

Input the number as a string. Then iterate over the string once and check if any character is bigger than the next character. If you find such a position, then just decrease that particular character by 1 and then make one iteration over the rest of the string and set all the characters to 9. You need to handle those numbers having leading zeros(A number cannot have zeros in any intermediate position, this will violate the non-decreasing property). This will give you the largest number less than N which is non-decreasing. You can find the pseudocode below :-

    string s;
    input(s);
    for(i=0; i (less than) s.size()-1; i++)
    {
        if(s[i] (greater than) s[i+1]) {
            s[i] = s[i]-1;
            for(j=i+1; j (less than) s.size(); j++)
                s[j] = '9';
            break;
        }
    }
    if(s.size() == 1) print(s); 
    else {
        for(i=0; i (less than) s.size(); i++)
        {
            if(s[i] != '0')
                print(s[i]);
        }
    }

(The less than and greater than sign cannot be printed here. So I have mentioned them in words.)
Hope this helps. :slight_smile:

1 Like

Here is a somewhat similar problem :slight_smile:

Thanks for your efforts :slight_smile:

Thanks for your efforts :slight_smile:

1 Like