Given a number N and a digit D, we have to form an expression or equation that contains only D and that expression evaluates to N. Allowed operators in an expression are +, -, *, and / . Find the minimum length expression that satisfies the condition above and D can only appear in the expression at most 10(limit) times. Hence limit the values of N (Although the value of limit depends upon how far you want to go. But a large value of limit can take a longer time for the below approach).
Remember, there can be more than one minimum expression of D that evaluates to N but the length of that expression will be minimum.
Input : N = 7, D = 3 Output : 3/3+ 3 + 3 Explanation : 3/3 = 1, and 1+3+3 = 7 This is the minimum expression. Input : N = 7, D = 4 Output : (4+4+4)/4 + 4 Explanation : (4+4+4) = 12, and 12/4 = 3 and 3+4 = 7 Also this is the minimum expression. Although you may find another expression but that expression can have only five 4's Input : N = 200, D = 9 Output : Expression not found! Explanation : Not possible within 10 digits.
The approach we use is Backtracking. We start with the given Digit D and start multiplying, adding, subtracting, and dividing if possible. This process is done until we find the total as N or we reach the end and we backtrack to start another path. To find the minimum expression, we find the minimum level of the recursive tree. And then apply our backtracking algorithm.
Let’s say N = 7, D = 3
The above approach is exponential. At every level, we recurse 4 more ways (at-most). So, we can say the time complexity of the method is where n is the number of levels in the recursive tree (or we can say the number of times we want D to appear at-most in the expression which in our case is 10).
Note: We use the above approach two times. First to find the minimum level and then to find the expression that is possible at that level. So, we have two passes in this approach. We can get the expression in one go, but you’ll need to scratch your head for that.
Expression: (4+4+4)/4+4 Expression: (((7+7)*7)*7+7+7)/7 Expression not found!
- Building Expression tree from Prefix Expression
- Smallest N digit number whose sum of square of digits is a Perfect Square
- Minimum count of numbers required from given array to represent S
- Stack | Set 4 (Evaluation of Postfix Expression)
- Expression contains redundant bracket or not
- Evaluate an array expression with numbers, + and -
- Arithmetic Expression Evalution
- Check for balanced parentheses in an expression | O(1) space
- What is an Expression and What are the types of Expressions?
- Find the minimum value of X for an expression
- Solve the Linear Equation of Single Variable
- Smallest number with given sum of digits and sum of square of digits
- Generate all N digit numbers having absolute difference as K between adjacent digits
- Match a pattern and String without using regular expressions
- Boggle | Set 2 (Using Trie)
- Print all permutation of a string using ArrayList
- Check for balanced parenthesis without using stack
- Infix to Prefix conversion using two stacks
- Rat in a Maze | Backtracking using Stack
- Infix to Postfix using different Precedence Values for In-Stack and Out-Stack
Improved By : thecodingpanda