Skip to main content

Maximum Sum Score of Array - Solution & Explanation

MediumPremiumFree on FleetCodeArrayPrefix Sum8 min readAsked at: Amazon
Practice this problem

Problem Statement

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.

 

Example 1:

Input: nums = [4,3,-2,5]
Output: 10
Explanation:
The sum score at index 0 is max(4, 4 + 3 + -2 + 5) = max(4, 10) = 10.
The sum score at index 1 is max(4 + 3, 3 + -2 + 5) = max(7, 6) = 7.
The sum score at index 2 is max(4 + 3 + -2, -2 + 5) = max(5, 3) = 5.
The sum score at index 3 is max(4 + 3 + -2 + 5, 5) = max(10, 5) = 10.
The maximum sum score of nums is 10.

Example 2:

Input: nums = [-3,-5]
Output: -3
Explanation:
The sum score at index 0 is max(-3, -3 + -5) = max(-3, -8) = -3.
The sum score at index 1 is max(-3 + -5, -5) = max(-8, -5) = -5.
The maximum sum score of nums is -3.

 

Constraints:

  • n == nums.length
  • 1 <= n <= 105
  • -105 <= nums[i] <= 105

Approach Overview

Problem Overview: You are given an integer array nums. For every index i, calculate a score defined as the larger value between the prefix sum nums[0..i] and the suffix sum nums[i..n-1]. The goal is to return the maximum score among all indices.

Approach 1: Brute Force Prefix/Suffix Calculation (O(n²) time, O(1) space)

The direct approach recomputes sums for every index. For each i, iterate from the start of the array to compute the prefix sum and iterate from i to the end to compute the suffix sum. Take max(prefix, suffix) as the score for that index and keep track of the best value. This approach uses only simple iteration over the array, but repeated summation makes it inefficient for large inputs.

Approach 2: Prefix Sum + Total Sum (O(n) time, O(n) space)

Precompute prefix sums using a prefix sum array. Let prefix[i] represent the sum of elements from 0 to i. The suffix sum starting at i can be derived from the total array sum as total - prefix[i-1]. Iterate through the array once more and compute the score at each index as max(prefix[i], suffix[i]). This removes redundant summation and keeps all operations constant time per index.

Approach 3: Running Prefix with Derived Suffix (O(n) time, O(1) space)

The optimal solution avoids storing a full prefix array. First compute the total sum of the array. Then iterate once while maintaining a running prefix sum. At index i, the prefix sum is the accumulated value so far and the suffix sum is total - prefix_before_i. Compute the score using max(prefix, suffix) and update the global maximum. This approach uses constant extra memory and a single pass after computing the total.

Recommended for interviews: The running prefix sum approach is the expected solution. Interviewers want to see recognition of the prefix–suffix relationship and the ability to derive suffix sums from the total sum. Starting with the brute force explanation shows problem understanding, but optimizing with prefix sums demonstrates algorithmic maturity.

Solution

We can use two variables l and r to represent the prefix sum and suffix sum of the array, respectively. Initially, l = 0 and r = sum_{i=0}^{n-1} nums[i].

Next, we traverse the array nums. For each element x, we add x to l and update the answer ans = max(ans, l, r), then subtract x from r.

After the traversal, return the answer ans.

The time complexity is O(n), where n is the length of the array nums. The space complexity is O(1).

Code

Python

Java

C++

Go

TypeScript

Rust

JavaScript

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Brute Force Prefix and Suffix RecalculationO(n²)O(1)Useful for understanding the scoring definition or verifying small test cases
Prefix Sum ArrayO(n)O(n)Good when prefix values are reused multiple times or needed for debugging
Running Prefix with Total Sum (Optimal)O(n)O(1)Best general solution with minimal memory usage

Video Solution

2219. Maximum Sum Score of Array (Leetcode Medium)Programming Live with Larry174 views views

Watch 2 more video solutions →

Frequently Asked Questions

Is Maximum Sum Score of Array easy or hard?
Maximum Sum Score of Array is considered a medium difficulty problem. The logic is straightforward once you recognize that suffix sums can be derived from the total sum, but identifying this prefix-sum optimization is the key challenge.
Maximum Sum Score of Array Python/Java solution
Most implementations compute the total sum first, then iterate while maintaining a running prefix sum. At each index, calculate the suffix using the total and update the maximum score. The same logic works across Python, Java, C++, Go, TypeScript, Rust, and JavaScript.
How to solve Maximum Sum Score of Array in O(n)?
First compute the total sum of the array. Iterate through the array while maintaining a running prefix sum. At each index i, compute the suffix as total minus the prefix sum before i, then evaluate max(prefix, suffix). Track the maximum value seen during the traversal.
What is the best approach for Maximum Sum Score of Array?
The best approach uses a running prefix sum combined with the total array sum. At each index, compute the prefix sum and derive the suffix sum using total minus the previous prefix. The score is max(prefix, suffix). This method runs in O(n) time and uses O(1) extra space.
Is Maximum Sum Score of Array asked at Google/Amazon/Meta?
Problems based on prefix sums and array scoring patterns frequently appear in interviews at companies like Amazon, Google, and Meta. While this exact problem may not always appear, the prefix–suffix sum technique tested here is a common interview pattern.
What data structure is used in Maximum Sum Score of Array?
The problem primarily uses arrays and the prefix sum technique. No advanced data structures are required. The optimal implementation only keeps a few integer variables to track the running prefix sum and total sum.
What is the time complexity of Maximum Sum Score of Array?
The optimal solution runs in O(n) time because the array is scanned a constant number of times. A brute force method that recomputes prefix and suffix sums for every index takes O(n²) time. Space complexity can be reduced to O(1) using a running prefix sum.

Ready to solve this problem?

Practice Maximum Sum Score of Array with our built-in code editor and test cases.

Practice on FleetCode