Message on Whatsapp 8879355057 for DSA(OA + Interview) + Fullstack Dev Training + 1-1 Personalized Mentoring to get 10+LPA Job
0 like 0 dislike
857 views
in Online Assessments by Expert (46,090 points) | 857 views

2 Answers

0 like 0 dislike
Best answer

images of ques
imageimage

by Expert (46,090 points)
1 like 0 dislike
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define pii pair<int,int>
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define all v.begin(),v.end()
int eval(vector<char> &v){
    if(v.size()==1){
        return (v[0]-'0');
    }
    if(v.size()==2){
        if(v[1]=='!'){
            return !(v[0]-'0');
        }
    }
    int f1=v[0]-'0';
    int f2=v[1]-'0';
    if(v[2]=='&'){
        return (f1&f2);
    }
    if(v[2]=='|'){
        return (f1|f2);
    }
    return 1;
}
int solve(vector<char>v){
    stack<char> st;
    int n=v.size();
    // for(auto it:v){
    //     cout<<it<<endl;
    // }
    for(int i=0;i<n;i++){
        if(v[i]==']'){
            vector<char> tt;
            while( st.top()!='['){
                tt.push_back(st.top());
                st.pop();
            }
            st.pop(); 
            st.push(eval(tt)+'0');//eval will only contain either 1,2  or 3 guys
        }
        else{
            st.push(v[i]);
        }
    }
    // while(!st.empty()){
    //     // cout<<st.top()<<endl;
    //     st.pop();
    // }
    return st.top()-'0';
}
int main(){
    string s;
    cin>>s;
    vector<char> v;
    for(auto it:s){
        if(it!=','){
            v.push_back(it);
        }
    }
    cout<< solve(v);
}
by Expert (46,090 points)