Given an integer array, find a continuous subarray where the sum of numbers is the biggest. Your code should return the index of the first number and the index of the last number. (If their are duplicate answer, return the minimum one in lexicographical order)
Example 1:
Example 1:
Input: [-3, 1, 3, -3, 4] Output: [1, 4]Example 2:
Input: [0, 1, 0, 1] Output: [0, 3] Explanation: The minimum one in lexicographical order.Method: We will extend kadane's algorithm to solve this problem.
vectorTime Complexity: O(n)continuousSubarraySum(vector &A) { int max_sum = A[0], sum = 0, first = 0, last = 0; for(int i=0; i < A.size(); i++) { if(sum >= 0) { sum += A[i]; } else { sum = A[i]; first = i; } if(sum > max_sum) { max_sum = sum; last = i; } } return vector {first, last}; }