Given an integer array nums
, find the subarray with the largest sum, and return its sum.
Example 1:
Input: nums = [-2,1,-3,4,-1,2,1,-5,4] Output: 6 Explanation: The subarray [4,-1,2,1] has the largest sum 6.
Example 2:
Input: nums = [1] Output: 1 Explanation: The subarray [1] has the largest sum 1.
Example 3:
Input: nums = [5,4,-1,7,8] Output: 23 Explanation: The subarray [5,4,-1,7,8] has the largest sum 23.
Constraints:
1 <= nums.length <= 105
-104 <= nums[i] <= 104
Follow up: If you have figured out the O(n)
solution, try coding another solution using the divide and conquer approach, which is more subtle.
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force strategy for finding the largest sum from a group of numbers involves checking every possible consecutive grouping of numbers. It's like trying out all possible slices of a loaf of bread to see which slice is the heaviest. We check each possible slice and keep track of the largest slice sum we've encountered so far.
Here's how the algorithm would work step-by-step:
def maximum_subarray_brute_force(numbers):
maximum_sum_so_far = float('-inf')
# Iterate through all possible start indices.
for start_index in range(len(numbers)):
current_sum = 0
# Iterate through all possible end indices starting from start_index.
for end_index in range(start_index, len(numbers)):
current_sum += numbers[end_index]
# Update maximum_sum_so_far if current_sum is greater.
maximum_sum_so_far = max(maximum_sum_so_far, current_sum)
return maximum_sum_so_far
The most efficient way to find the maximum sum of a continuous set of numbers involves tracking the current best sum as we go. If the current set of numbers starts hurting the sum, we simply start a new set.
Here's how the algorithm would work step-by-step:
def max_subarray(numbers):
current_max = numbers[0]
overall_max = numbers[0]
for i in range(1, len(numbers)):
# Decide if extending the subarray helps.
current_max = max(numbers[i], current_max + numbers[i])
# Update overall max sum if needed
overall_max = max(overall_max, current_max)
return overall_max
Case | How to Handle |
---|---|
Null or empty input array | Return 0 or throw an IllegalArgumentException as there are no subarrays to sum. |
Array with a single element | Return the value of that single element as it's the maximum possible subarray sum. |
Array with all negative numbers | Return the largest negative number in the array, as that's the best possible contiguous sum. |
Array with all zeros | Return 0, as the contiguous subarray [0] is the maximum possible sum. |
Array with large positive and negative numbers (potential integer overflow) | Use a data type that can accommodate larger sums, like long in Java or Python. |
Array with highly skewed positive and negative numbers, concentrated in specific regions | Kadane's algorithm efficiently handles this case by continuously updating the maximum sum. |
Maximum subarray spanning almost the entire array with a small negative element interrupting it | Kadane's algorithm will correctly identify and consider or disregard the interruption to maximize the sum. |
Very large array size (performance consideration) | Kadane's algorithm has O(n) time complexity and constant space complexity, making it suitable for large arrays. |