Easy and Understandable code


#1
vector<int> Solution::maxset(vector<int> &v) {
vector<int> ans;
long int max_sum = INT_MIN;
long int curr_sum = 0;
int g_s_i,g_e_i,s_i,e_i;
g_s_i = g_e_i = s_i = e_i = 0;
for(int i =0;i<v.size();i++){
	if(v[i]<0){
		curr_sum =0;
		s_i = i + 1;
	}
	else{
		curr_sum += v[i];
		if(curr_sum == max_sum){
			if(i - s_i + 1 > g_e_i - g_s_i + 1){
				g_e_i = i;
				g_s_i = s_i;
			}
		}
		else if(curr_sum > max_sum){
			max_sum = curr_sum;
			g_s_i =s_i;
			g_e_i = i;
		}			
	}
}
for(int i = g_s_i ;i <= g_e_i ;i++) ans.push_back(v[i]);
return max_sum < 0 ? vector<int>() : ans;

}