# Continuous Subarray Sum

## Question

### Problem Statement

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 anyone)

#### Example

Give [-3, 1, 3, -3, 4], return [1,4].

## 题解

### Java

public class Solution {
/**
* @param A an integer array
* @return  A list of integers includes the index of the first number and the index of the last number
*/
public ArrayList<Integer> continuousSubarraySum(int[] A) {
ArrayList<Integer> result = new ArrayList<Integer>();
if (A == null || A.length == 0) return result;

int sum = 0, minSum = 0, maxSub = Integer.MIN_VALUE;
int first = 0, last = 0;
int first2 = 0; // candidate for first
for (int i = 0; i < A.length; i++) {
if (minSum > sum) {
minSum = sum;
first2 = i;
}
sum += A[i];
if (sum - minSum > maxSub) {
maxSub = sum - minSum;
last = i;
// update first if valid
if (first2 <= last) first = first2;
}
}

return result;
}
}