WA in Maximum Number (MAXNUM3) LTIME53

Can someone give a test case where my code fails or rather guide me to the correct approach ?

My solution

After taking care of the last digit cases, I’m using a greedy approach to find the first number that on removal will lead to (sum of digits) %3=0 and has the next number greater than itself. If such a number doesn’t exist, I remove the lowest rightmost number that will lead to that result.

havent taken care of the case when last%2 == 0 and l2%2 == 0.

why u define soo much macro’s?

That’s when the greedy approach is used.

consider the case 3 960. the answer should be 96

I applied the same technique, passed only subtask2.

Didn’t understand where the approach is wrong.


  [1]: https://www.codechef.com/viewsolution/15992745

In your code on line 73:

x < a[i+1]-‘0’ this condition is wrong because it doesn’t guarantee that the resulting number after deleting a digit will be greater than the one you found earlier.

Consider this test case:





Your Output



Correct Output



Can anyone tell me due to which testcase it was showing wrong answer?





Correct Output:


Your Output:

1 Like

If you want another approach, Have a look here.

If you wannna find bug in your approach, explain it in a bit detail. :slight_smile:

I’m noob in java so I can’t help you with why your code gives no output but I can say that you’ve read the question wrong.
I see you’ve taken input as integer which is wrong because the input constraint says that input number can be of 10^5 digits long i.e input number can be 10^(10^5) which your long int input can’t handle.
Try implementing using string.

Hope it helps.

thanks @a_d_i

Can anyone give me a test case where my code fails? It’s working well for all the test cases I checked.


I’m storing the digits in an array from one’s place. Then I am iterating through all the digits and finding the new number by removing that particular digit. Now, I’m checking whether it is divisible by 6 or not and if it is then I check whether it is largest number possible which I initially set to -1. At last I’m printing the new number with k-1 digits where k is the total number of digits in the initial number. Leading zeros will be printed since I used %0*d and passed k-1 and maxnum to it.
LINK:- My Solution

It would time out.

The solution time complexity is O(N^2) which won’t pass.

Further, the number can have 10^5 digits, thus, can’t be stored in int or even long long.

But it’s giving wrong answer

n can be 100000 so it can only have a maximum of 6 digits right?

n can have 100000 digits .

So you can’t store it in int

But in constraints it is giving n>=2

You’re welcome bro.