Digit Inversion

Easy
0/40
Average time to solve is 10m
profile
Contributed by
1 upvote
Asked in company
Ola

Problem statement

Digit Inverse of a number is a number which can be found by replacing 0 with 9, 1 with 8, 2 with 7, 3 with 6, 4 with 5 and vice versa.

Can you find the digit inverse of the given number 'N'?

Example:
Let 'N' = 1234567

Replacing 1 with 8, 2 with 7, 3 with 6, 4 with 5, 5 with 4, 6 with 3, 7 with 2. Thus the digit inverse of 'N' will be 8765432 as we replaced each digit in 'N'.
Detailed explanation ( Input/output format, Notes, Images )
Input format:
The first line of input contains an integer β€˜T’ denoting the number of test cases.

The first line of each test case contains an integer β€˜N’ representing the number whose digit inverse is to be found.
Output format :
For each test case, return an integer denoting the digit inverse of the given number.
Note :
You don't need to print anything, it has already been taken care of. Just implement the given function.
Constraints:
1 <= T <= 10^5
0 <= N < 10^9

Time limit: 1 sec
Sample Input 1:
2
9364
9999
Sample Output 1:
635
0
Explanation for Sample Output 1:
In test case 1,

1. Replace 9 with 0
2. Replace 3 with 6
3. Replace 6 with 3
4. Replace 4 with 5

Thus the digit inverse of 9364 will be 635.

In test case 2,

1. Replace 9 with 0
2. Replace 9 with 0
3. Replace 9 with 0
4. Replace 9 with 0

Thus the digit inverse of 9999 will be 0.
Sample Input 2:
2
426738
89347
Sample Output 2:
573261
10652
706152
Explanation for Sample Output 1:
In test case 1,

1. Replace 4 with 5
2. Replace 2 with 7
3. Replace 6 with 3
4. Replace 7 with 2
5. Replace 3 with 6
6. Replace 8 with 1

Thus the digit inverse of 426738 will be 573261.

In test case 2,

1. Replace 8 with 1
2. Replace 9 with 0
3. Replace 3 with 6
4. Replace 4 with 5
5. Replace 7 with 2

Thus the digit inverse of 89347 will be 10652.
Hint

The approach is simple: just replace each digit with its inverse digit.

Approaches (2)
Digit Replacing

The approach is simple: just replace each digit with its inverse digit.

 

All we need to do is just traverse over each digit in the number and replace it with its digit inverse. We can get the last digit of a number by taking its modulo with 10 and then we will divide the number by 10 so that we can access the next digit. Simultaneously we will create a new number which is the digit inverse of the given number.

Time Complexity

O(log10(N)), Where β€˜N’ is the given number.

 

Since we will be traversing over each digit of the number. Thus the time complexity will be O(log10(N)).

Space Complexity

O(1)

 

Since we are using constant extra space. Thus the space complexity will be O(1).

Code Solution
(100% EXP penalty)
Digit Inversion
Full screen
Console