Magic Number Transformation

Given an integer number n, you are allowed to invert its digits. Inverting a digit t means replacing it with 9 - t. You need to transform the initial number n to the minimum possible positive number by inverting some (possibly, zero) digits.

Note:

  • The decimal representation of the final number should not start with a zero.
  • Your goal is to minimize the number by inverting some of its digits.

Example 1:

Input: x = 27

Output: 22

Explanation:

In this example, the digit 7 can be inverted to 2 (as 9 - 7 = 2), resulting in the minimum possible number 22.

Example 2:

Input: x = 53

Output: 43

Explanation:

Here, the digit 5 can be inverted to 4 (as 9 - 5 = 4), resulting in the minimum possible number 43.

Now Your Turn!

Pick the correct output for the given input

Input: x = 87

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 <= n <= 1018

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

0
class Solution {
public:
long long magicTransformation(long long n) {
// Your code goes here
}
};
Test Case

Input:

X