The Wayback Machine - https://web.archive.org/web/20211020072701/https://www.geeksforgeeks.org/find-minimum-value-expression-by-inserting-addition-or-multiplication-operator-between-digits-of-given-number/
Skip to content
Related Articles

Related Articles

Improve Article

Find minimum value expression by inserting addition or multiplication operator between digits of given number

  • Last Updated : 19 Oct, 2021
Geek Week

Given string str that contains only digits, the task is to return an expression by inserting the ‘+’ or ‘*’ operator between every two digits such that the arithmetic value of the expression is minimized.

Example: 

Input: str = “322”
Output: “3+2*2”
Explanation: The value of above expression is 7 which is minimum possible over the other expressions “3+2+2”, “3*2+2”, “3*3*2”

Input: str = “391118571”
Output: “3+9*1*1*1+8+5+7*1”

 

Approach: The given problem can be solved using a greedy approach. Follow the steps below to solve the problem:



  • If the string str contains character ‘0’ then:
    • Insert multiplication operator between every character of the string
  • Else create a string ans to store the iterate the string and if the current character str[i] is:
    • ‘1’, then Insert multiplication operator ‘*’ between the current and previous character
    • not ‘1’ then Insert addition operator ‘+’ between the current and previous character

Below is the implementation of the above approach:

C++




// C++ implementation for the above approach
  
#include <bits/stdc++.h>
using namespace std;
string minimalExpression(string str)
{
    // Initialize an empty string
    // to store the answer
    string ans = "";
  
    ans.push_back(str[0]);
  
    bool iszero = false;
  
    // Check if zero is present in the
    // string
    for (int i = 0; i < str.size();
         i++) {
        if (str[i] == '0') {
            iszero = true;
        }
    }
  
    // Insert all multiplication operators
    // between every digit of the string
    if (iszero) {
        for (int i = 1; i < str.size();
             i++) {
            ans.push_back('*');
            ans.push_back(str[i]);
        }
        return ans;
    }
  
    // Else calculate minimum expression
    // with combination of '*' and '+'
    // operators between digits
    for (int i = 1; i < str.size(); i++) {
  
        // If current character is '1' insert
        // multiplication operator between
        // current and previous character
        if (str[i] == '1') {
            ans.push_back('*');
            ans.push_back(str[i]);
        }
  
        // Else insert addition operator
        // between current and
        // previous character
        else {
            ans.push_back('+');
            ans.push_back(str[i]);
        }
    }
  
    // Return the result
    return ans;
}
  
// Driver Code
int main()
{
    string str = "391118571";
    cout << minimalExpression(str);
}
Output
3+9*1*1*1+8+5+7*1

Time Complexity: O(N)
Auxiliary Space: O(N), where N is the length of the given string

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :