from Hacker News

Balance LHS = RHS

by hackerdad on 9/7/17, 6:11 AM with 0 comments

While helping my 5th grader kid, I came across a question from his math text book about using parenthesis to evaluate the expression.

The questions and examples were simple enough for fifth grader but the generalization of the same seems like a hard problem.

Here is the problem when generalized:

1. You are given an equation of the form LHS = RHS.

2. LHS is a mathematical expression on real numbers with +, -, * and / operations.

3. RHS is a single numerical value (real number).

4. LHS != RHS

Given above, modify the LHS such that by adding one or more groups of parenthesis, LHS == RHS.

Find the Big O time complexity for the algorithm

Example 1

Input

200 - 60 / 5 = 28

Output

(200 - 60)/5 = 28

Example 2

Input

12 + 3 * 17 - 3 * 2 = 108

Output

(12 + 3 * (17 - 3)) * 2 = 108

Hoping to start a thread discussing the solutions