Largest Sum in Any Consecutive Sequence
Dynamic ProgrammingAuto-graded · 21 test casesLast updated
Tests understanding of dynamic programming through finding the maximum sum achievable by selecting adjacent elements from an array.
Problem Statement
You are given an array of integers. Your task is to identify the consecutive sequence of elements whose sum is larger than any other consecutive sequence, and return that maximum sum.
The sequence must contain at least one element.
Constraints
1 <= len(nums) <= 10^5-10^9 <= nums[i] <= 10^9- The answer will always fit within a 64-bit signed integer.
Notes
- A consecutive sequence means elements that appear next to each other in the original array order.
- When all elements are negative, the answer is the single largest (least negative) element.
- You must select at least one element; the empty sequence is not allowed.
Examples
Example 1
Input: largest_consecutive_sum([-2, 1, -3, 4, -1, 2, 1, -5, 4])
Output: 6
Example 2
Input: largest_consecutive_sum([-5, -1, -8])
Output: -1
Hints
Hint 1
Consider what happens at each position: should you extend the sequence you've been building, or start fresh from the current element?
Hint 2
Track two values as you scan left to right: the best sum ending exactly at the current position, and the overall best sum seen so far.
Hint 3
If carrying forward the previous sum would make things worse, it's better to reset and begin a new sequence at the current element.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
This problem is solved using a dynamic programming technique where we make a single pass through the array while maintaining two key pieces of information.
At each position, we decide whether to:
- Continue building on the sequence we've accumulated so far by adding the current element
- Abandon the previous sequence and start a new one from the current element
The decision rule is simple: if the accumulated sum from before is negative, adding it to the current element only makes things worse. In that case, we're better off starting fresh.
We track:
running_sum: the maximum sum of any sequence that ends exactly at the current positionmax_sum: the best sum we've encountered at any position so far
Why This Works
Every valid consecutive sequence ends at some index. By the time we process position i, we know the optimal sequence ending at position i-1. Any sequence ending at i either:
- Extends the optimal sequence from
i-1by includingnums[i] - Starts fresh at
nums[i]alone
Taking the maximum of these two options gives us the optimal sequence ending at i. By tracking the best value across all positions, we find the global maximum.
Initializing both variables with nums[0] ensures we never accidentally consider the empty sequence (which would happen if we started at zero and all elements were negative).
Complexity Analysis
Time Complexity: O(n) where n is the length of the array. We make exactly one pass through the elements, performing constant-time operations at each step.
Space Complexity: O(1). We only maintain two integer variables regardless of input size. By iterating with an index rather than slicing, we avoid creating any copies of the array.
Full Solution
from typing import List
def largest_consecutive_sum(nums: List[int]) -> int:
# Initialize with first element to guarantee non-empty sequence
running_sum = max_sum = nums[0]
for i in range(1, len(nums)):
# Either extend the previous sequence or start fresh
running_sum = max(nums[i], running_sum + nums[i])
# Update the global maximum
max_sum = max(max_sum, running_sum)
return max_sum