How can i improve upon my Solution?


#1

my code: -

#include
vector Solution::maxset(vector &A) {
int size = A.size();
int startindex = INT_MIN, endindex = INT_MIN;
int s_ans = INT_MIN, e_ans = INT_MIN;
int count = 0 , count_ans = 0;
long long sum = 0,sum_of_ans = 0;

for(int i = 0 ; i < size;i++){
if(A[i] >= 0){
sum = sum + A[i];
if(startindex == INT_MIN){
startindex = i;
}
endindex = i;
count++;

}else if(A[i] < 0){
if(sum > sum_of_ans){
sum_of_ans = sum;
s_ans = startindex;
e_ans = endindex;
count_ans = count;
}else if(sum == sum_of_ans){
if(count > count_ans){
sum_of_ans = sum;
s_ans = startindex;
e_ans = endindex;
count_ans = count;
}else if(count == count_ans){
if(startindex < s_ans){
sum_of_ans = sum;
s_ans = startindex;
e_ans = endindex;
count_ans = count;

        }
    }
} 

startindex = INT_MIN;
endindex = INT_MIN;
sum = 0;
count = 0 ;

}
}

if(sum > sum_of_ans){
sum_of_ans = sum;
s_ans = startindex;
e_ans = endindex;
count_ans = count;
}else if(sum == sum_of_ans){
if(count > count_ans){
sum_of_ans = sum;
s_ans = startindex;
e_ans = endindex;
count_ans = count;
}else if(count == count_ans){
if(startindex < s_ans){
sum_of_ans = sum;
s_ans = startindex;
e_ans = endindex;
count_ans = count;

        }
    }
}

vector ans;

if(s_ans != INT_MIN){
for(int i = s_ans; i <= e_ans; i++){
ans.push_back(A[i]);

}
}
return ans;
}