Replace X
Practice
4.7 (3 votes)
Basic math
Number theory
Algorithms
Math
Problem
89% Success 597 Attempts 20 Points 1s Time Limit 256MB Memory 1024 KB Max Code

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 <= 10^6\)

 

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:20
2 votes
Tags:
Easy
Points:20
2 votes
Tags:
ImplementationMathematicsEasyMathematicsMathamatics
Points:20
15 votes
Tags:
Ad-HocBasic ProgrammingOpenApprovedEasyMathamatics