![]() |
VOOZH | about |
Given a large integer N, the task is to find the largest possible integer which is divisible by 9 after inserting exactly one digit from 0 to 9 anywhere in the N.
Note: Leading zeroes are not allowed.
Examples:
Input: N = "12"
Output: 612
Explanation: The numbers which are divisible by 9 after inserting digit are 612, 162, 126.
And the largest integer is 612. So the output is 612Input: N = "1346182944512412414214"
Output: 81346182944512412414214
Approach: The problem can be solved with the help of the below observation:
The idea is to insert the digit 0 to 9 at every position in the N and check is it divisible by 9 (divisible only if sum of digits is divisible by 9).
If it is found to be true then store the maximum value with position where digit is inserted and finally print the maximum value.
Follow the below steps to solve the problem:
Below is the implementation of the above approach:
612
Time Complexity: O(N)
Auxiliary Space: O(1)