Coding Games and Programming Challenges to Code Better

Send your feedback or ask for help here!

Thanks for nice puzzle

I am passing all tests inside the test but i canâ€™t pass â€śNeed more lengthâ€ť test on validation step.

Have no ideas what i have made wrong (

Is anybody here who passed it? Any ideas?

got it)

all tests passed now

but you didnâ€™t share any resolution

2 Likes

The puzzle stub provided a long datatype to get the input value. This could be a trick (intended or unintended) to mislead people thinking it in the wrong way - trying to solve it by using long or BigInt.

You can modify it to read the input as a string and process it as string or char array - it might be more straight forward in algorithm and also has the benefit to receive and process inputs of nearly unlimited length.

in Digit sum successor which is a medium level puzzle, my code times out in 5th and 7th test cases! Iâ€™m actually incrementing the number, get its sum, and then compare it to the sum of the N whichâ€™s brute forcing. So how can I make it more efficient? Thanks in advance

2 Likes

You should not brute force it.

Primary school maths (about grade 1 or 2) has exercises like these:

â€śArrange the digits 1,7,2 and 1 to create the lowest possible four-digit number.â€ť

â€śWhat is the next bigger number you can create from the same digits?â€ť

No kid should need to brute force to try out all combinations to get the correct answers. Neither should you.

When you can solve these kid exercises, you are not too far away from solving this â€ścreate-number-from-digitsâ€ť puzzle.

But those only work because youâ€™re given a limited set of digits to use to construct your answer. In this CodinGame puzzle, youâ€™re only given a number, but not a list of digits, so thereâ€™s an unlimited number of possibilities.

â€śGiven a numberâ€ť so that it can be converted into a list of digits.

â€śthereâ€™s an unlimited number of possibilitiesâ€ť is not really true, as the statement said we need only the â€śsmallest number greater than Nâ€ť

We also have some known facts or constraints:

- suppose the number is 1234, the increment will not happen in digit 4. It must be in some digits before the last one. (My theory, to be proved by anyone interested in it.)
- if you inc a digit, you must dec some other digits.

So what I did was to find out which digit is best to make the inc. And then find out how to allocate the excessive value to dec some other digits. Nothing brute force.

1 Like

Thank you Java.

I tried working with long long and even by optimising to the maximum , a single validation test doesnt pass.

We have definetely have to use strings.