Question: Meesho, Recent Online Assessment (NIT Jamshedpur , 6M + FTE) | Trading Firm | An Array of Positive Integers | Source Array
2
Entering edit mode

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

Question-1:

int helper(int x, int y, int miny, int maxy, vector<int>& v){
    int i = x, j = x-1;
    int a = 0, b = 0;
    int temp = 0;
    while(i<=y && j<=y){
        while(j+1<=y && (a==0 || b==0)){
            j++;
            if(v[j] == miny)a++;
            else if(v[j] == maxy)b++;
        }
        if(a && b){
            temp += y-j+1;
        }
        if(v[i] == miny)a--;
        else if(v[i] == maxy)b--;
        i++;
    }
    return temp;
}

void solve(int testCase){
    int n; cin>>n;
    int miny, maxy; cin>>miny>>maxy;
    vector<int> v(n);
    fo(i,0,n)cin>>v[i];
    int ans = 0;
    int x = -1, y = -1;
    bool ok = 0;
    fo(i,0,n){
        if(v[i] < miny || v[i] > maxy){
            if(ok){
                ans += helper(x, y, miny, maxy, v);
            }
            ok = 0;
        }else{
            if(!ok){
                x = i;
                ok = 1;
            }else{
                y = i;
            }
        }
    }
    if(ok){
        ans += helper(x, y, miny, maxy, v);
    }
    cout<<ans<<endl;
}

ADD COMMENTlink 15 months ago Hariprasath • 0

Login before adding your answer.

Similar Posts
Loading Similar Posts