Given an array of the size n integer which is finding the maximum sum of the subarray of an array
class Solution {
public:
int maxSubarraySumCircular(vector<int>& nums) {
int n=nums.size();
int maxi=INT_MIN, mini=INT_MAX;
int sum=0;
int tmpmax=0, tmpmin=0;
for(int i=0; i<n; i++){
sum+=nums[i];
tmpmax+=nums[i];
maxi=maxi<tmpmax ? tmpmax:maxi;
tmpmax=tmpmax<0 ? 0:tmpmax;
tmpmin+=nums[i];
mini=mini>tmpmin? tmpmin:mini;
tmpmin=tmpmin>0 ? 0:tmpmin;
}
if(sum==mini){
return maxi;
}
return max(maxi, (sum-mini));
}
};
0 Comments