Short and simple C++ solution SS


#1
using ll = long long;

vector<int> Solution::maxset(vector<int> &A) {
    ll n = A.size();
    ll x=0, y=0, s=0, mx=0, my=0, ms=0;
    for(ll i=0;i<n;i++){
        if(A[i]<0) x=i+1, y=i+1, s=0;
        else {
            s+=A[i], y++;
            if(s>ms || ms>=s && mx==x)
                ms=s, mx=x, my=y;
        }
    }
    vector<int> V(A.begin()+mx, A.begin()+my);
    return V;
}

#2

could someone explain the variables


#3
vector<int> Solution::maxset(vector<int> &A) {
    vector<vector<int>>v;
    int count=0,max_index=0;
    long long max_sum=-1,sum=0;
    vector<int> temp,max_temp;
    for(int i=0;i<A.size();i++)
    {
        if(A[i]>=0)
        {
            sum+=A[i];   
            temp.push_back(A[i]);
            if(sum>max_sum||((max_sum==sum)&&max_temp.size()<temp.size()))
            {
                max_temp.clear();
                max_sum=sum;
                copy(temp.begin(), temp.end(), back_inserter(max_temp));
            }
        }
        else if(A[i]<0)
        {
            sum=0;
            temp.clear();
        }
    }
        
    
    return(max_temp);
}

#5

could you please explain the “if” condition inside the “else” .


#6

this program gives wrong output for A=[-1,0,0,0]…this program is returning empty array instead of 0,0,0


#7

can u explain the OR case in the if (ms>=s &&mx==x)


#8

simple correct output giving code in c++
vector Solution::maxset(vector &A) {
long maxsum=0;
long maxsofar=0;
vectortemp;
vectorvtemp;
for(int i=0;i<A.size();i++)
{

    if(A[i]>=0)
    {
        maxsum+=A[i];
        temp.push_back(A[i]);
        if((maxsofar<maxsum)||((maxsum==maxsofar)&&(temp.size()>vtemp.size())))
        {
        maxsofar=maxsum;
        vtemp.clear();
        vector<int>::iterator i;
        for(i=temp.begin();i!=temp.end();i++)
        {
          vtemp.push_back(*i);
        }
        }
        
    }
    else if(A[i]<0) 
    {
        temp.clear();
        maxsum=0;
    }
    
}
return vtemp;

}