Question: Airtel Payment's Bank, Recent Online Assessment Questions (25th August 2023) | Minimum Difference of Sub Arrays | Path to element
1
Entering edit mode

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

//PATH TO ELEMENT

#include <bits/stdc++.h>
using namespace std;

string finder(vector<int> &v, int n, int idx){
    //have to find node n
    if(idx>=v.size()){
        return "";
    }
    
    string ans;
    if(v[idx]==n){
        return "";
    }
    
    if(v[idx] > n){
        //left child
        ans = 'L' + finder(v,n,2*idx+1);
    }
    
    else if(v[idx] < n){
        ans = 'R' + finder(v,n,2*idx+2);
    }
    
    return ans;
}

int main()
{
    int h;
    cin>>h;
    int sz = (1<<(h+1)) - 1;
    
    vector<int> v(sz);
    for(int i=0; i<sz; i++){
        cin>>v[i];
    }
    
    int n;
    cin>>n;
    cout<<finder(v,n,0)<<endl;
    
    return 0;
}

ADD COMMENTlink 4 months ago hehehaha • 10

Login before adding your answer.

Similar Posts
Loading Similar Posts