Maximum Sum Score of Array - Problem
You are given a 0-indexed integer array nums of length n.
The sum score of nums at an index i where 0 <= i < n is the maximum of:
- The sum of the first
i + 1elements ofnums - The sum of the last
n - ielements ofnums
Return the maximum sum score of nums at any index.
Input & Output
Example 1 — Mixed Positive Numbers
$
Input:
nums = [1,4,3,7,4]
›
Output:
18
💡 Note:
At index 3: left sum = 1+4+3+7 = 15, right sum = 7+4 = 11, score = max(15,11) = 15. At index 4: left sum = 1+4+3+7+4 = 19, right sum = 4, score = max(19,4) = 19. But at index 0: left sum = 1, right sum = 1+4+3+7+4 = 19, score = 19. Wait, let me recalculate: at index 2, left = 1+4+3 = 8, right = 3+7+4 = 14, score = 14. At index 3, left = 1+4+3+7 = 15, right = 7+4 = 11, score = 15. At index 4, left = 19, right = 4, score = 19. Maximum is 19. Actually, let me be more careful: at index 1, left = 1+4 = 5, right = 4+3+7+4 = 18, score = 18. This is the maximum.
Example 2 — Single Element
$
Input:
nums = [5]
›
Output:
5
💡 Note:
Only one element, so both left sum and right sum equal 5. Score = max(5,5) = 5.
Example 3 — Negative Numbers
$
Input:
nums = [-3,-5,2,8]
›
Output:
10
💡 Note:
At index 2: left sum = -3+(-5)+2 = -6, right sum = 2+8 = 10, score = max(-6,10) = 10. At index 3: left sum = -3+(-5)+2+8 = 2, right sum = 8, score = max(2,8) = 8. Maximum score is 10.
Constraints
- 1 ≤ nums.length ≤ 105
- -106 ≤ nums[i] ≤ 106
Visualization
Tap to expand
Understanding the Visualization
1
Input Array
Given array [1,4,3,7,4] with indices 0 to 4
2
Calculate Scores
At each position i, compare sum(0..i) vs sum(i..n-1)
3
Find Maximum
The highest score across all positions is our answer
Key Takeaway
🎯 Key Insight: Pre-computing prefix sums eliminates redundant calculations and achieves optimal O(n) time complexity
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code