Code not passing three test cases

Can you please look at my code.
import java.util.Scanner;

public class series1 {
public static void main(String args[]) {
Scanner scn = new Scanner(System.in);

	long n = scn.nextLong();

	long[] arr = new long[18];

	int count = 0;

	while (n != 0) {
		long rem = n % 10;
		arr[count] = rem;
		count++;
		n = n / 10;
	}

	// count will tell total no. of digits

	for (int i = 0; i < count; i++) {

		if (i != count - 1) {
			if (arr[i] >= 5) {
				arr[i] = 9 - arr[i];
			}
		} else {
			if (arr[i] >= 5 && arr[i] <= 8) {
				arr[i] = 9 - arr[i];
			}
		}
	}

	for (int i = count - 1; i >= 0; i--) {
		if(arr[i]==0)
		{
			arr[i]=arr[i]+1;
			System.out.print(arr[i]);
		}
		else
		{
		System.out.print(arr[i]);
		}
	}

}

}

@rg361 you have missed one condition.
There are many numbers form after inverting the digit. For minimum number, check if inverting digit is less than or greater than the original digit. If it is less, then invert it otherwise leave it. Just check if changed no. is smaller if it is then change it otherwise use older one. Use below approach ;

  • 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 long int in c++ and 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.

Can you please share the correct code, I’ve got the logoc but I’m not able to excecute it.

@rg361 Here is my code for your reference. Try to understand it only then code the problem on your own.

import java.util.*;
import java.lang.Math;

public class Main{
  public static void main(String[] args){
    Scanner scn = new Scanner(System.in);
        long n = scn.nextLong();
       System.out.println(smallest_num(n));
    }

  public static long smallest_num(long num){
    long ans = 0;
    long mult = 1;
    while(num != 0){
      long rem = num % 10;
      if(rem >= 5){
        if(rem == 9 && (num/10) == 0){
         //do nothing
        }else{
          rem = 9 - rem;
        }
      }
      ans += rem * mult;
      mult *= 10;
      num /= 10;
    }
    return ans;
  }
}

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.