Minimum Number That Can Be Obtained By Applying '+' And '*' Operations On Array Elements


Problem Statement

We have given an array of length ‘N’ containing some positive integers. Also, we have given the string of length ‘N-1’ containing only ‘*’ and ‘+’ characters, where ‘*’ is a multiplication operator, and ‘+’ is an addition operator. We require to perform the arithmetic operation with array elements in such a way that we can get a minimum positive integer value.

Sample Examples

Input

array = [1,2,3], str = “*+”

Output

5

Explanation

It is the resultant value of ((1*2) + 3).

Input

array = [3, 3, 3, 3], str = ‘*++’

Output

15

Explanation

It performs array[0]*array[1], equal to 9, and says it result1. After that, it adds array[2] with result1, equal to 12, and says it result2. Next, it adds result2 to the array[3], equal to 15.

Input

array =  [1, 3, 5, 6], str = “**+”

Output

21

Explanation

It is a resultant value of ((1*3*5) + 6).

Approach 1

We will use the bit masking to solve the problem in this approach.

We can use bit masking whenever we have two choices for selection. Here, we require to apply arithmetic operations in any order, but we need to choose between multiplication and arithmetic operations from the given string

So, bit mask allows us to get all possible ways to arrange both arithmetic operators. After that, we can perform arithmetic operations for each way and check if the resultant value is minimum or not.

Let’s clear the above logic with example inputs. In the below example, array = [1. 3, 5, 6], and str = “*++”.

Here, the string length is 3. So, we can have a total of 8 (2^3) bitmasks, which are 000, 001, 010, 100, 110, 101, 011, 111.

Now, if we assume ‘1’ as a ‘*’ and ‘0’ as a ‘+’ operator, we can get all arrangements of the arithmetic operators given in the string.

However, we should use any arrangements only if the total number of ‘1’ is equal to the ‘*’ and ‘0’ is equal to the ‘+’ operator.

Algorithm

Users should follow the below algorithm to implement the above approach.

  • Step 1 − Define the ‘totalMul’ variable and initialize it with zero to store a total number of multiplication arithmetic operators in the string.

  • Step 2 − Use the for loop to iterate through the given string. If the current character is equal to the ‘*’, increase the value of the ‘totalMul’ variable by 1.

  • Step 3 − Use the for loop to get all possible bit masks for X equal to the string length. Here, ‘len’ is the array length, and ‘len – 1’ is a string length.

  • Step 4 − Define the ‘setBitCount’ variable to store the total number of set bits in the current mask. Also, define the ‘order’ list to store the current order of arithmetic operations.

  • Step 5 − In the for loop, use another for loop to get a total number of set bits (‘1’) in the current mask. Left shift 1 by ‘j’, and take the ‘&’ operation between the resultant value and ‘I’ to check whether the jth bit is set to bit.

  • Step 6 − If the current bit is a set-bit, increment the value of the ‘setBitCount’ variable and push the ‘*’ in the order vector; else, push the ‘+’ in the order vector.

  • Step 7 − If setBitCount’s value is equal to the totalMul’s value is same, it means we can use the current mask to arrange arithmetic operations; Otherwise, we move to the next iteration.

  • Step 8 − Inside the if statement, use the ‘deque’ data structure to define ‘currentQueue’ to store the array elements.

  • Step 9 − Iterate through the ‘order’ list. If the current character is ‘*’, pop the last element from the queue and multiply it with the element at the current array index.

  • Step 10 − If the current character is ‘+’ in the ‘orders’ list, push the current array element in the ‘currentQueue’.

  • Step 11 − Use the while loop to pop all elements from the ‘currentQueue’ and do the sum of all elements.

  • Step 12 − Use the min() function to get the minimum value from the ‘minimum’ and ‘sum’ variables.

Example

#include <bits/stdc++.h>
using namespace std;

// Function to get the minimum number by applying mathematical operations in any order.
int getMinimumSum(int array[], int len, string str){
   // to store a total number of multiplication operators in the string.
   int totalMul = 0;
   for (int i = 0; i < (int)str.size(); i++){
      // increment totalMul if the current character is '*'
      if (str[i] == '*')
         totalMul += 1;
      }
   // store maximum number value in the result.
   int minimum = 1000000;
   // Iterate over all the possible masks and find the minimum value by applying arithmetic operations.
   for (int i = 0; i < (1 << (len - 1)); i++){
      // to store the number of bits set in the mask.
      int setBitCount = 0;
      // to store the order of the operators.
      vector<char> order;
      // finding a total number of mask bits in the current mask 'i'. If the current bit is set to bit, push '*' in the order vector; else, push '+'.
      for (int j = 0; j < len - 1; j++){
         if ((1 << j) & (i)){
            setBitCount += 1;
            order.push_back('*');
         } else {
            order.push_back('+');
         }
      }
      // if the set bit count is equal to the total number of multiplication operators, then only apply the operations.
      if (setBitCount == totalMul){
         // queue to store the current elements.
         deque<int> currentQueue;
         // push the first element in the queue.
         currentQueue.push_back(array[0]);
         for (int j = 0; j < len - 1; j++) {
            // get the current operator from the order vector.
            if (order[j] == '*'){
               // if the current operator is '*', multiply the last element in the queue with the next element in the array.
               int temp = currentQueue.back();
               currentQueue.pop_back();
               temp = temp * array[j + 1];
               // push a new value
               currentQueue.push_back(temp);
            } else {
               // if current operator is '+', then push the next element in the array in the queue.
               currentQueue.push_back(array[j + 1]);
            }
         }
         int sum = 0;
         // Add all the elements in the queue.
         while (currentQueue.size() > 0){
            int temp = currentQueue.front();
            sum += temp;
            currentQueue.pop_front();
         }
         // get the minimum value.
         minimum = min(minimum, sum);
      }
   }
   return minimum;
}

int main(){
   int array[] = {1, 3, 5, 6};
   string str = "*++";
   int len = sizeof(array) / sizeof(array[0]);
   cout << "Minimum number value can be achieved is : " << getMinimumSum(array, len, str);
   return 0;
}

Output

Minimum number value can be achieved is : 14
  • Time complexity − O(2N-1*N), Where N is the array's length. As we iterate through all bitmasks, and inside that, we use for loop to count the total number of set bits in the current mask, it is O(2N-1*N).

  • Space complexity − O(N), As we use lists to store the order of arithmetic operators.

Updated on: 18-Jul-2023

34 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements