What is the result of 2991? Is it 12 or 1002?
What is the result of 999? Is it 0?
Code:
import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
long n = 0;
if(sc.hasNextLong())
n = sc.nextLong();
chewbacca(n);
}
public static void chewbacca(long n) {
int arr[] = new int[10];
int count = 0; //for all zeroes
while(n > 0) {
int r = (int) (n % 10);
count++;
if(r >= 5)
r = 9 - r;
arr[r]++;
n = n / 10;
}
if(arr[0] == count) //all 0s
System.out.print(0);
else {
StringBuilder sb = new StringBuilder();
for(int i = 1; i <= 9; i++) {
while(arr[i] > 0) {
sb.append(i);
arr[i]--;
}
}
System.out.print(sb);
}
}
}