Replace X

4.7

3 votes
, Basic Math, Number theory, Algorithms, Math
Problem

Bob is preparing for the Maths Olympiad and came across a problem in which Bob needs your help!

You are given a number N in the form of a string which has 2 missing digits, denoted by X. You've to compute and output the largest number one can get after replacing both X, such that the number is divisible by 3 but not by 9

Example

Assumption:

N = 24X5X

Approach:

We can replace the first X by 9 and the second X by 4 to get 24954 as our answer.

It should also be noticed that the largest number divisible by 3 after replacing the X is 24957, but since it was divisible by 9, therefore our answer is 24954.
 

Function description

Complete the function largestNumber provided in the editor. This function takes the following parameter and returns the required answer:
N: the number in the form of a string.
 

Input format

NoteThis is the input format that you must use to provide custom input (available above the Compile and Test button). 

The first and only line of the input contains a string N

Output format
The only line of the output should contain the largest number that can be made after replacing both the X, which is divisble by 3 but not by 9

Note: The number should be a positive number and should not contain any leading zeros.

Constraints

2<=N.length<=106

 

Sample Input
X3X51
Sample Output
93651
Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

In the sample test case of N=X3X51, we can replace the first X with 9 and the second one with a 6 to get the largest number divisible by 3 but not by 9, as 93651

Editor Image

?