![]() |
VOOZH | about |
Given an array arr[] consisting of N positive integers and a string S of length (N - 1), containing characters '+' and '*', the task is to find the smallest number that can be obtained after applying the arithmetic operations mentioned in the string S on the array elements in any order.
Examples:
Input: A[] ={2, 2, 2, 2}, S = "**+"
Output: 8
Explanation:
Operation 1: Perform the multiplication operation on the first two elements operation i.e., arr[0]*arr[1] = 2*2 = 4. Now the array modifies to {4, 2, 2}.
Operation 2: Perform the multiplication operation on the remaining two elements i.e., arr[1]*arr[2] = 2*2 = 4. Now the array modifies to {4, 4}.
Operation 3: Perform the addition operation on the remaining elements arr[0] + arr[1] = 4 + 4 = 8. Now the array modifies to {8}.
Therefore, the result of the operation performed is 8, which is minimum.Input: A[] = {1, 2, 3, 4}, S = "*++"
Output: 9
Approach: The given problem can be solved using bit-masking. Follow the steps below to solve the problem:
Below is the implementation of the above approach:
8
Time Complexity: O(2(N - 1)*N)
Auxiliary Space: O(N)