Chewbacca and number(Array section 1.0)

Sir,
I am unable to understand this question chewbacca and number of array 1.0 section.

Please explain me this problem ??

Hello @shivamdurani220,

In this question you have to find and print the smallest number possible by reversing digits of the input.

  1. As x <= 100000000000000000, use long long int datatype or for this specific question, you can use a string also as it is easier to iterate on digit of the number.

  2. Now starting from the leftmost digit in the number iterate till the rightmost digit, do the following:
    2.1. find the invert of the current digit s[i] i.e. 9-s[i]
    2.2. check if the invert is smaller than the actual digit:
    …if yes, then replace the digits[i] in the number with it’s invert.
    …if no, then don’t replace
    example 1:
    123
    inverting 1: 9-1=8
    as 8>1,
    so, if i would replace 1 with 8 (i,e 823), then the number will become larger.
    But, we are required to find the smallest, so don’t replace.
    example 2:
    632
    inverting 6: 9-6=3
    as 3<6,
    so, if i would replace 6 with 3 (i,e 332), then the number will become smaller.
    Thus, replace will be made.

  3. As it is mentioned that the leftmost digit can’t be 0, so we would not replace if a[0]=9
    example:
    912
    inverting 9: 9-9=0
    0<9,
    But, the number cannot start with 0.
    So, we won’t replace.

Hope, this would help.
Give a like, if you are satisfied.

1 Like

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.