Question: Goldman Sachs , Online Assessment | Binary Tree Magic Number Sum | Grid Operations | SDE Internship | MNIT Jaipur | 2024
1
Entering edit mode

ADD COMMENTlink 15 months ago PoGo 2.4k
0
Entering edit mode

Question 1 Solution

 

int ans=0;
void sol(int i,vector<int> &v)
{
    if(v[i]==-1)
        return;
    if(v[2*i+1]!=-1 && v[2*i+2]!=-1)
    {
        int x=v[2*i+1];
        int y=v[2*i+2];
        if(x%y==0 || y%x==0)
            ans+=v[i];
    }
    sol(2*i+1,v);
    sol(2*i+2,v);
}
void solve()
{
int n;
cin>>n;
vector<int> v(1e5,-1);
int root;
cin>>root;
v[0]=root;
for(int i=0;i<n-1;i++)
{
    string x;
    cin>>x;
    int val;
    cin>>val;
    int pos=0;
    for(auto j:x)
    {
        if(j=='L')
            pos=2*pos+1;
        else
            pos=2*pos+2;
    }
    v[pos]=val;
}
sol(0,v);
cout<<ans<<endl;

}

 

ADD COMMENTlink 15 months ago suryansh jaiswal • 370

Login before adding your answer.

Similar Posts
Loading Similar Posts