#2219
Medium Algorithms Maximum sum score of array
Array Prefix Sum
62.6% acceptance
Mar 31, 2026
72
19
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.
Solution
Rust
Time O(n)
Space O(1)
impl Solution {
pub fn maximum_sum_score(nums: Vec<i32>) -> i64 {
let n = nums.len();
let total: i64 = nums.iter().map(|&x| x as i64).sum();
let mut prefix = 0i64;
let mut ans = i64::MIN;
for i in 0..n {
prefix += nums[i] as i64;
let suffix = total - prefix + nums[i] as i64;
ans = ans.max(prefix.max(suffix));
}
ans
}
}