[Community Puzzle] Digit sum successor

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:

  1. 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.)
  2. 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.