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: 19
💡 Note: At index 0: left=[1] sum=1, right=[1,4,3,7,4] sum=19, score=19. At index 4: left=[1,4,3,7,4] sum=19, right=[4] sum=4, score=19. Maximum score across all indices is 19.
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 of Array INPUT nums array (n=5) 1 i=0 4 i=1 3 i=2 7 i=3 4 i=4 Sum Score Definition: Left sum: sum(nums[0..i]) Right sum: sum(nums[i..n-1]) Score at i = max(left, right) Total Sum = 1+4+3+7+4 = 19 Track: prefixSum, totalSum suffixSum = totalSum - prefixSum ALGORITHM STEPS 1 Calculate Total Sum totalSum = 19 2 Initialize Variables prefixSum=0, maxScore=MIN 3 Single Pass Loop Update prefix, calc suffix i prefix suffix score max 0 1 19 19 19 1 5 18 18 19 2 8 15 15 19 3 15 11 15 19 4 19 4 19 19 * suffix at i=3: 7+4=11, but 4 Return Maximum maxScore = 19 FINAL RESULT Best Score Found at i=0 1 4 3 7 4 At i=0: Left sum = 1 Right sum = 1+4+3+7+4 = 19 Score = max(1, 19) = 19 This is the maximum! OUTPUT 19 OK - Maximum Sum Score Time: O(n), Space: O(1) Key Insight: Instead of computing prefix and suffix sums separately, use the relation: suffixSum = totalSum - prefixSum + nums[i] This allows O(1) space complexity by only tracking the running prefix sum and total sum. TutorialsPoint - Maximum Sum Score of Array | Space-Optimized Single Pass Approach
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