3
int minOperationsToFlip(string exp) {4
stack<pair<char, int>> st; // exp val,cost9
if (x == '(' || x == '&' || x == '|') {10
st.push({x, 0}); // till now no looking need to change exp so mark cost11
// as 0 or we can add cost of these while solving exp16
st.pop(); // this pop for '('18
// if number or we can say '0' or '1'19
p = {x, 1}; // cost going to be one becoz x if either '0' or '1'21
while (!st.empty() && (st.top().first == '&' || st.top().first == '|')) {22
char op = st.top().first;26
char val1 = st.top().first;27
int cost1 = st.top().second;31
if (val1 == '1' && val2 == '1') {32
p = {'1', min(cost1, cost2)};34
if (val1 == '1' && val2 == '0') {37
if (val1 == '0' && val2 == '1') {40
if (val1 == '0' && val2 == '0') {41
p = {'0', min(1 + cost1, 1 + cost2)};46
if (val1 == '1' && val2 == '1') {47
p = {'1', 1 + min(cost1, cost2)};49
if (val1 == '1' && val2 == '0') {52
if (val1 == '0' && val2 == '1') {55
if (val1 == '0' && val2 == '0') {56
p = {'0', min(cost1, cost2)};63
return st.top().second;