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 + 1 elements of nums
  • The sum of the last n - i elements of nums

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
Maximum Sum Score: Find Best Split Position1437401234At index 1 (highlighted):Left: 1+4 = 5Right: 4+3+7+4 = 18Score = max(5, 18) = 18Maximum Score: 18Check all positions to find the maximum
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
Asked in
Google 25 Amazon 18 Microsoft 15 Apple 12
23.0K Views
Medium Frequency
~15 min Avg. Time
850 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen