Given a string that contains only digits from 0 to 9, and an integer value, **target**. Find out how many expressions are possible which evaluate to **target** using binary operator +, – and * in given string of digits.

Input :"123", Target : 6Output :{“1+2+3”, “1*2*3”}Input :“125”, Target : 7Output :{“1*2+5”, “12-5”}

This problem can be solved by putting all possible binary operator in mid between to digits and evaluating them and then check they evaluate to target or not.

- While writing the recursive code, we need to keep these variable as argument of recursive method – result vector, input string, current expression string, target value, position till which input is processed, current evaluated value and last value in evaluation.
- Last value is kept in recursion because of multiplication operation, while doing multiplication we need last value for correct evaluation.

See below example for better understanding –

Input is 125, suppose we have reached till 1+2 now, Input = “125”, current expression = “1+2”, position = 2, current val = 3, last = 2 Now when we go for multiplication, we need last value for evaluation as follows: current val = current val - last + last * current val First we subtract last and then add last * current val for evaluation, new last is last * current val. current val = 3 – 2 + 2*5 = 11 last = 2*5 = 10

Another thing to note in below code is, we have ignored all numbers which start from 0 by imposing a condition as first condition inside the loop so that we will not process number like 03, 05 etc.

See the use of c_str() function, this function converts the C++ string into C char array, this function is used in below code because atoi() function expects a character array as an argument not the string. It converts character array to number.

`// C++ program to find all possible expression which ` `// evaluate to target ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Utility recursive method to generate all possible ` `// expressions ` `void` `getExprUtil(vector<string>& res, string curExp, ` ` ` `string input, ` `int` `target, ` `int` `pos, ` ` ` `int` `curVal, ` `int` `last) ` `{ ` ` ` `// true if whole input is processed with some ` ` ` `// operators ` ` ` `if` `(pos == input.length()) ` ` ` `{ ` ` ` `// if current value is equal to target ` ` ` `//then only add to final solution ` ` ` `// if question is : all possible o/p then just ` ` ` `//push_back without condition ` ` ` `if` `(curVal == target) ` ` ` `res.push_back(curExp); ` ` ` `return` `; ` ` ` `} ` ` ` ` ` `// loop to put operator at all positions ` ` ` `for` `(` `int` `i = pos; i < input.length(); i++) ` ` ` `{ ` ` ` `// ignoring case which start with 0 as they ` ` ` `// are useless for evaluation ` ` ` `if` `(i != pos && input[pos] == ` `'0'` `) ` ` ` `break` `; ` ` ` ` ` `// take part of input from pos to i ` ` ` `string part = input.substr(pos, i + 1 - pos); ` ` ` ` ` `// take numeric value of part ` ` ` `int` `cur = ` `atoi` `(part.c_str()); ` ` ` ` ` `// if pos is 0 then just send numeric value ` ` ` `// for next recurion ` ` ` `if` `(pos == 0) ` ` ` `getExprUtil(res, curExp + part, input, ` ` ` `target, i + 1, cur, cur); ` ` ` ` ` ` ` `// try all given binary operator for evaluation ` ` ` `else` ` ` `{ ` ` ` `getExprUtil(res, curExp + ` `"+"` `+ part, input, ` ` ` `target, i + 1, curVal + cur, cur); ` ` ` `getExprUtil(res, curExp + ` `"-"` `+ part, input, ` ` ` `target, i + 1, curVal - cur, -cur); ` ` ` `getExprUtil(res, curExp + ` `"*"` `+ part, input, ` ` ` `target, i + 1, curVal - last + last * cur, ` ` ` `last * cur); ` ` ` `} ` ` ` `} ` `} ` ` ` `// Below method returns all possible expression ` `// evaluating to target ` `vector<string> getExprs(string input, ` `int` `target) ` `{ ` ` ` `vector<string> res; ` ` ` `getExprUtil(res, ` `""` `, input, target, 0, 0, 0); ` ` ` `return` `res; ` `} ` ` ` `// method to print result ` `void` `printResult(vector<string> res) ` `{ ` ` ` `for` `(` `int` `i = 0; i < res.size(); i++) ` ` ` `cout << res[i] << ` `" "` `; ` ` ` `cout << endl; ` `} ` ` ` `// Driver code to test above methods ` `int` `main() ` `{ ` ` ` `string input = ` `"123"` `; ` ` ` `int` `target = 6; ` ` ` `vector<string> res = getExprs(input, target); ` ` ` `printResult(res); ` ` ` ` ` `input = ` `"125"` `; ` ` ` `target = 7; ` ` ` `res = getExprs(input, target); ` ` ` `printResult(res); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

Output:

1+2+3 1*2*3 1*2+5 12-5

This article is contributed by **Utkarsh Trivedi**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

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.

## Recommended Posts:

- Queries to evaluate the given equation in a range [L, R]
- Number of ways to calculate a target number using only array elements
- Minimum steps to reach target by a Knight | Set 2
- Burn the binary tree starting from the target node
- Count the number of ways to construct the target string
- Balanced expressions such that given positions have opening brackets | Set 2
- Number of balanced bracket expressions that can be formed from a string
- How to validate a Username using Regular Expressions in Java
- Print all possible combinations of r elements in a given array of size n
- Print all possible strings of length k that can be formed from a set of n characters
- Given a string, print all possible palindromic partitions
- Print all possible strings that can be made by placing spaces
- Print all possible combinations of the string by replacing '$' with any other digit from the string
- Print all possible paths from the first row to the last row in a 2D array
- 0/1 Knapsack Problem to print all possible solutions
- Print all possible paths in a DAG from vertex whose indegree is 0
- Print all possible ways to write N as sum of two or more positive integers
- Print all Possible Decodings of a given Digit Sequence
- Print all possible strings that can be made by placing spaces
- Print all possible words from phone digits