{x}
blog image

Maximum Sum Score of Array

Solution Explanation: Maximum Sum Score of Array

This problem asks to find the maximum sum score of an array at any index. The sum score at an index i is the maximum of the sum of the first i+1 elements and the sum of the last n-i elements (where n is the array length).

The most efficient approach uses prefix sums implicitly. Instead of calculating prefix and suffix sums explicitly (which would take O(n) space), we maintain running totals.

Algorithm:

  1. Calculate Total Sum: First, calculate the total sum (r) of all elements in the array. This represents the initial sum of the last n elements.

  2. Iterate and Update: Iterate through the array. For each element:

    • Add the current element to the running sum of the first i+1 elements (l).
    • Update the maximum sum score (ans) by comparing it with the current values of l (sum of the first i+1 elements) and r (sum of the last n-i elements).
    • Subtract the current element from r to update the sum of the remaining (last n-i-1) elements.
  3. Return Maximum: After iterating through all elements, return the maximum sum score (ans).

Time Complexity: O(n) - We iterate through the array once.

Space Complexity: O(1) - We use a constant amount of extra space to store variables.

Code Examples (with explanations):

Python:

class Solution:
    def maximumSumScore(self, nums: List[int]) -> int:
        l, r = 0, sum(nums)  # Initialize left sum (initially 0) and right sum (total sum)
        ans = -float('inf')  # Initialize answer to negative infinity
        for x in nums:
            l += x          # Add current element to left sum
            ans = max(ans, l, r)  # Update maximum sum score
            r -= x          # Subtract current element from right sum
        return ans

Java:

class Solution {
    public long maximumSumScore(int[] nums) {
        long l = 0, r = 0;
        for (int x : nums) r += x; // Calculate total sum
        long ans = Long.MIN_VALUE; // Initialize answer to the minimum long value
        for (int x : nums) {
            l += x;
            ans = Math.max(ans, Math.max(l, r)); // Update maximum
            r -= x;
        }
        return ans;
    }
}

C++:

class Solution {
public:
    long long maximumSumScore(vector<int>& nums) {
        long long l = 0, r = accumulate(nums.begin(), nums.end(), 0LL); //accumulate calculates sum
        long long ans = -1e18; // Initialize to a very small number
        for (int x : nums) {
            l += x;
            ans = max({ans, l, r}); //Use max to find the largest of three values
            r -= x;
        }
        return ans;
    }
};

The other languages (JavaScript, TypeScript, Go, Rust) follow a very similar pattern, adapting the syntax appropriately for each language but maintaining the same core logic of iterating through the array, updating running sums, and tracking the maximum score. The key is the efficient use of running totals to avoid the need for explicitly calculating and storing prefix and suffix sums.