Parenthesize An Expression To Produce A Certain Value

Revision en1, by 013, 2016-04-27 07:37:16

Given an expression E and another integer 'k' , you are required to find out whether it is possible to parenthesize E such that it evaluates to 'k'. If the answer is YES, print the corresponding parenthesized expression. Assume the max and min value of E will be within -N and +N where N ≤ 10000 and the value of  - N ≤ k ≤ N.

Sample Input and output:

Input: E = 2 + 3 * ( - 1) + 4 + ( - 7), k = 8

Output: Yes. E = (2 + 3) * (( - 1) + 4) + ( - 7) = 8

Can this problem be solved in O(n2N)? If yes then how? If no then what is the best complexity to solve it?

Tags parenthesis, dp

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en1 English 013 2016-04-27 07:37:16 607 Initial revision (published)