Given an arithmetic expression, find all possible outcomes of this expression. Different outcomes are evaluated by putting brackets at different places.
We may assume that the numbers are single digit numbers in given expression.
Input: 1+3*2 Output: 8 7 Explanation (1 + 3)*2 = 80 (1 + (3 * 2)) = 70 Input: 1*2+3*4 Output: 14 20 14 20 20 (1*(2+(3*4))) = 14 (1*((2+3)*4)) = 20 ((1*2)+(3*4)) = 14 ((1*(2+3))*4) = 20 ((1*2)+3)*4) = 20
The idea is to iterate through every operator in given expression. For every operator, evaluate all possible values of its left and right sides. Apply current operator on every pair of left side and right side values and add all evaluated values to the result.
1) Initialize result 'res' as empty. 2) Do following for every operator 'x'. a) Recursively evaluate all possible values on left of 'x'. Let the list of values be 'l'. a) Recursively evaluate all possible values on right of 'x'. Let the list of values be 'r'. c) Loop through all values in list 'l' loop through all values in list 'r' Apply current operator 'x' on current items of 'l' and 'r' and add the evaluated value to 'res' 3) Return 'res'.
Below is the implementation of above algorithm.
14 20 14 20 20
Exercise: Extend the above solution so that it works for numbers with multiple digits also. For example, expressions like “100*30+20” (Hint: We can create an integer array to store all operands and operators of given expression).
This article is contributed by Ekta Goel. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Find the minimum value of the given expression over all pairs of the array
- Minimum LCM and GCD possible among all possible sub-arrays
- Find all the possible remainders when N is divided by all positive integers from 1 to N+1
- Find product of all elements at indexes which are factors of M for all possible sorted subsequences of length M
- Find the missing digit x from the given expression
- Find a permutation of 2N numbers such that the result of given expression is exactly 2K
- Find minimum value of the expression by choosing K elements from given array
- Parity of the given mathematical expression using given N numbers
- Find all Pairs possible from the given Array
- Find all possible ways to Split the given string into Primes
- Find the minimum value of X for an expression
- Find Range Value of the Expression
- All possible values of floor(N/K) for all values of K
- Print the balanced bracket expression using given brackets
- Maximize the value of the given expression
- Minimum possible value T such that at most D Partitions of the Array having at most sum T is possible
- Find all possible coordinates of parallelogram
- Find the maximum possible value of a[i] % a[j] over all pairs of i and j
- Find the sum of all possible pairs in an array of N elements
- Find the sum of the costs of all possible arrangements of the cells