← 返回 google 的题目列表Maximum Sum Subarray with Equal Endpoints
类型:online_judge
Problem: Maximum Subarray Sum with Equal Endpoints
Given an integer array a, find a pair of indices (i, j) such that:
0 <= i <= j < n
a[i] == a[j]
Among all such pairs, maximize the subarray sum:
S(i,j) = a[i] + a[i+1] + ... + a[j]
Return the indices i j that achieve the maximum sum.
If multiple pairs achieve the same maximum sum, you may return any of them.
Constraints
1 <= n <= 2e5
-1e9 <= a[i] <= 1e9
Follow-up
Keep extra space strictly O(1) (excluding the input array). You may allow higher time complexity (e.g., O(n log n) or O(n^2)), but additional memory must remain constant.
Examples / Tests
a = [1, 2, 3, 1] → best i=0, j=3, sum = 7
a = [5] → i=0, j=0, sum = 5
a = [2, -1, 2] → i=0, j=2, sum = 3
a = [1, -100, 1, 1] → one best is i=2, j=3, sum = 2
a = [3, 3, -10, 3] → best could be i=0, j=1, sum = 6
Example
Input
4
1 2 3 1
Output
0 3