Minimum number that can be obtained by applying ' + ' and ' * ' operations on array elements | Function to find the smallest number that can be obtained after applying the arithmetic operations mentioned in the string S ; Stores the count of multiplication operator in the string ; Store the required result ; Iterate in the range to create the mask ; Checking the number of bits that are set in the mask ; Check if the number of bits that are set in the mask is multiplication operation ; Storing the elements that is to be added ; Apply the multiplications operation first ; If sign is ' * ' , then multiply last element of deque with arr [ i ] ; Push last multiplied element in the deque ; If the element is to be added , then add it to the deque ; Add all the element of the deque ; Minimize the answer with the given sum ; Driver Code
function minimumSum ( A , N , S ) { let mul = 0 ; for ( let i = 0 ; i < S . length ; i ++ ) { if ( S [ i ] == " " ) mul += 1 ; } let ans = 1000000 ; for ( let i = 0 ; i < 1 << ( N - 1 ) ; i ++ ) { let cnt = 0 ; let v = [ ] ; for ( let j = 0 ; j < N - 1 ; j ++ ) { if ( ( 1 << j ) & i ) { cnt += 1 ; v . push ( " " ) ; } else { v . push ( " " ) ; } } if ( cnt == mul ) { let d = [ ] ; d . push ( A [ 0 ] ) ; for ( let j = 0 ; j < N - 1 ; j ++ ) { if ( v [ j ] == " " ) { let x = d [ d . length - 1 ] ; d . pop ( ) ; x = x * A [ j + 1 ] ; d . push ( x ) ; } else { d . push ( A [ j + 1 ] ) ; } } let sum = 0 ; while ( d . length > 0 ) { let x = d [ 0 ] ; sum += x ; d . shift ( ) ; } ans = Math . min ( ans , sum ) ; } } return ans ; } let A = [ 2 , 2 , 2 , 2 ] ; let S = " " ; let N = A . length ; document . write ( minimumSum ( A , N , S ) ) ;