Misleading contraints! - Easy cpp sulution O(n)


#1

the contraints for the problem says the A[i]>=1 && A[i]<=1e5 , but there exist cases which contains A[i]>=1e10. Please correct the constraints, (if possible).

Solution-

#define ll long long int
vector Solution::maxset(vector &A) {
int n=A.size();
vector res;
int i;
ll sum=0;
ll o_sum=0;
int start=0;
int s=0,e=0;
int end=0;
int prev_len=0;
for(i=0;i<n;){
if(A[i]>=0)
s=i;
while(A[i]>=0){
if(i==n)
break;
sum+=A[i];
i++;
}
e=i-1;
if(o_sum<=sum){
int temp_s=start;
int temp_e=end;
int prev_o_sum=o_sum;
prev_len=temp_e-temp_s;
o_sum=sum;
if(e-s<=prev_len && prev_o_sum>=o_sum){
start=temp_s;
end=temp_e;
}
else{
start=s;
end=e;
}
}
sum=0;
i++;
}
for(i=0;i<n;i++){
if(A[i]>=0)
break;
}
if(i==n)
return res;
for(i=start;i<=end;i++)
res.push_back(A[i]);
return res;
}