Given an integer N, the task is to print all the possible arithmetic expressions using all numbers from 1 to N and with binary operator +, –, * and /.
Input: n = 2
1+2, 1-2, 1/2, 1*2
Input: n = 3
1+2+3, 1+2-3, 1+2/3, 1+2*3, 1-2+3, 1-2-3, 1-2/3, 1-2*3
1/2+3, 1/2-3, 1/2/3, 1/2*3, 1*2+3, 1*2-3, 1*2/3, 1*2*3
- We will create a character array of length = n + n – 1, because for an expression with n operands to be valid we will need n-1 operators.
- Iterate the array and put numbers at even position whereas symbols at the odd position and call the function recursively.
- If number of characters becomes equal to the length of array, print the array.
Below is the implementation of the above approach:
1+2 1-2 1/2 1*2
- Removing a number from array without changing its arithmetic mean
- Find the missing number in Arithmetic Progression
- Find the missing number in unordered Arithmetic Progression
- Check if two expressions with brackets are same
- Check if the given Binary Expressions are valid
- How to validate a Password using Regular Expressions in Java
- Print Bracket Number
- Program to print Even Odd Number Pyramid
- Print next greater number of Q queries
- Print number in ascending order which contains 1, 2 and 3 in their digits.
- Queries to multiply the given subarray with given number X and print sum
- Print a number as string of 'A' and 'B' in lexicographic order
- Print all K digit repeating numbers in a very large number
- Print all 3 digit repeating numbers in a very large number
- Print number with commas as 1000 separators in Python
- Print number of words, vowels and frequency of each character
- Print all the sum pairs which occur maximum number of times
- Queries to print the character that occurs the maximum number of times in a given range
- Print shortest path to print a string on screen
- Longest Arithmetic Progression | DP-35
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.