Answer the question
In order to leave comments, you need to log in
How to solve the problem of placing arithmetic signs between numbers to get another number?
Given N integers, the task is to place + and - signs between them to obtain the number S while preserving the original sequence of numbers in the expression, or to prove the impossibility of obtaining such S. For example, if the numbers are - [7, 3, 9] and S = 13, then the answer that satisfies the problem = 7-3+9=13
How can such an algorithm be implemented?
Answer the question
In order to leave comments, you need to log in
A full enumeration can be considered each character as a bit: 0 is a plus, 1 is a minus. It turns out that it is necessary to sort through all the numbers from 0
to 2^(n-1)
and their binary notation encodes signs.
For optimization, you can first check some extreme conditions:
>= S
. To immediately cut off the check of options for obtaining 1E9 from only 1E6 units. The same check makes sense in the process of enumeration, to assess whether the remaining digits reach the sum remaining up to SDidn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question