Join Regular Classroom : Visit ClassroomTech

MountBlue Solution | PlayGame Code | Codewindow.in

Write the following code in the def portion and rest are unchanged | C++

#codewindow.in

int playGame(vector<int> stars)
{
    int n=stars.size();
    int right[n];
    if(stars[n-1]==1)
    right[n-1]=1;
    else
    right[n-1]=-1;
    for(int i=n-2;i>=0;i--)
    {
        if(stars[i]==1)
        right[i]=right[i+1]+1;
        else
        right[i]=right[i+1]-1;
    }
    int c=0,s=0;
    for(int i=0;i<n;i++)
    {
        if(s>right[i])
        break;
        else
        {
            c++;
            if(stars[i]==1)
            s++;
            else
            s--;
        }
    }
    return max(0,c);
}
#end

Follow us

Also Checkout