Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.
Example:
Input: [-2,1,-3,4,-1,2,1,-5,4], Output: 6 Explanation: [4,-1,2,1] has the largest sum = 6.
class Solution {
public:
int maxSubArray(vector<int>& nums) {
int max=INT_MIN;
int result;
int i,j;
if(nums.size()==1)
return nums[0];
if(nums.size()==0)
return 0;
for(i=0;i<nums.size();i++)
{
for(j=i;j<nums.size();j++)
{
result=accumulate(nums.begin()+i,nums.begin()+j+1,0);
if(result>max)
max=result;
}
}
return max;
}
};
It has passed 200/202 test cases but got time limit extended issue on the rest 2 testcases.How do I optimize this?