Search code examples
algorithmcomplexity-theoryreductionnp

Complexity of an old Top Coder riddle: Making a number by inserting +


This is a follow up to my previous question (about an old top coder riddle).

Given a string of digits, find the minimum number of additions required for the string to equal some target number. Each addition is the equivalent of inserting a plus sign somewhere into the string of digits. After all plus signs are inserted, evaluate the sum as usual.

For example, consider "303" and a target sum of 6. The best strategy is "3+03".

I guess (not proved it though) the problem is NP-complete. What do you think? How would you reduce a well-known NP-complete problem to this problem?


Solution

  • If the base is made a parameter, then there is a reduction from subset sum. Let x1, …, xn, s > 0 be the instance of subset sum and let S = x1 + … + xn. In base S + 1, let the Top Coder input be

    x1 0 x2 0 … xn 0

    summing to (S - s) (S + 1) + s.

    Much more interesting of course is the hardness of the base 10 case.