Minimum pluses required to make the equation (x = y) correct

4

Problem Statement:

Given an equation “x=y”, for example, “111=12”, you need to add pluses inside x to make the equation correct. In our example “111=12”, we can add one plus “11+1=12” and the equation becomes correct. You need to find the minimum number of pluses to add to x to make the equation correct. If there is no answer print -1.

Note that the value of y won’t exceed 5000. The numbers in the corrected equation may contain arbitrary amounts of leading zeros.

Input Format The first line contains a string, A as described in the problem statement.

Constraints 1 <= len(A) <= 10^3

I tried the recursive approach. Which is for every character in the 'x', I have two options I can include the plus sign next to the current digit or move to the next digit (by including the current digit with the next digit) I checked all the combinations and found the minimum pluses. As you know, this is exponential in complexity. I'm not able to apply dynamic programming for this problem as I can't think of the states for the dynamic programming.

I know this problem can be solved by dynamic programming. But, I don't know how to identify the state and the transition.

Share
Improve this question
5
  • 2
    This is not the right site to ask for help on solving general problems. It's a site for asking specific questions about programming. – super Apr 12 at 16:03
  • 8
    @super: Huh? This is a specific algorithmic problem, which is entirely on-topic for SO according to the tour (it lists "Software algorithms"). The only problem with this question is that the OP didn't show their attempts at solving it. – Yakov Galka Apr 12 at 16:06
  • @YakovGalka If you want to call copy-pasting some programming quiz/problem a specific algorithmic problem you are more generous then me. Also, I think that fact that it fails to show any attempt at a solution or what parts of the problem OP is having an issue with makes it anything but specific. I could have worded that more carefully or precise, but I guess seeing this kind of post dumps 15 times a day makes you less eager to do so. – super Apr 12 at 16:14
  • 1
    It does not show any effort, that part is true and also it should be more specific. However, this does qualify a programming problem though. @Naveen Kumar Please mention your efforts in the question and be more specific on the part where you are facing problem. – AKSingh Apr 12 at 16:21
  • 1
    @AKSingh, Thanks for your response. I tried the recursive approach. Which is for every character in the 'x', I have two options I can include the plus sign next to the current digit or move to the next digit (by including the current digit with the next digit) I checked all the combinations and found the mininum pluses. As you know, this is exponential in complexity. I'm not able to apply dynamic programming for this problem as I can't think of the states for the dp. – Naveen Kumar Apr 12 at 16:39

Comments

Popular posts from this blog

Meaning of `{}` for return expression

Get current scroll position of ScrollView in React Native

flutter websocket connection issue