INVRDIG - Editorial

PROBLEM LINK: Div-3 Contest

Author: Bhagya , Rishika
Tester: Rishika
Editorialist: Bhagya

DIFFICULTY:

EASY

PREREQUISITES:

Strings and Arrays

PROBLEM:

invert the digits of a number such that number would become minimum

QUICK EXPLANATION:

invert the digits of number “n” such that number would become minimum. Inverting digit d means replacing it with digit 9 - d. find the minimum number and the final answer shouldn’t start with zero.

EXPLANATION:

Little Chintu is a smart and naughty kid. He was bothering his teacher in class. so that, his teacher gives him a number n and told him to invert the digits of that number such that number would become minimum. Inverting digit d means replacing it with digit 9 - d. And the final answer shouldn’t start with zero. Can you help Chintu to find minimum number?

Constraints

0 <= n <= 100000000000000000

SOLUTION:

import java.util.*;
public class Main{
	public static void main(String[] args)
	{
		Scanner sc = new Scanner(System.in);
		long n = sc.nextLong();
 
		long ans = 0;
		long mul = 1;
 
		while(n >= 10)
		{
			int rem = (int)(n % 10);
			long min = Math.min(rem, 9-rem);
			ans = min * mul + ans;
			mul = mul * 10;
			n = n /10;
		}
 
		if(n == 9)
			ans = 9 * mul + ans;
		else
		{
			ans = Math.min(n,  9- n) * mul + ans;
		}
 
		System.out.println(ans);
 
		sc.close();
	}
}