How to do this question?

how to do this question? i’m not getting any thing

@Harsh_Sonwani,

The logic behind this Problem was pretty simple as we can invert any digit ( 9 - digit) but we need to invert only such digits that will eventually end up giving the smallest number possible.

So, we should invert only digits greater then or equal to 5 as after inverting them, the result gives us smallest number.

For e.g.,

9 - 5 = 4 viz, smaller than the original number that was 5.

9 - 8 = 1 viz, smaller than the original number that was 8.

but 9 - 1 = 8 viz, greater than the original number that was 1.

Important point to consider is, After inverting any digits their should not be trailing zeros that means, if their is 9 at the starting of the number then it must remain the same

Also input can be quite big so, u need to capture the number in long in java.

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.

give me the code i can do it. i have tried a lot but failed in some test cases

import java.util.Scanner; public class Main { public static void main(String args[]) { Scanner scn =new Scanner(System.in); long m=scn.nextInt(); long s; long r = 0; while (m != 0) { s = m % 10; long q = 9 - s; if (s > q) s = q; else s = s; r = r * 10 + s; m = m / 10; } mm®; } public static void mm(long n) { long s; long r = 0; while (n != 0) { s = n % 10; r = r * 10 + s; n = n / 10; } System.out.println®; } }

@Harsh_Sonwani
Tell me what’s your doubt