Skip to main content
All Questions

Find the maximum subarray sum.

Medium

Given an array of integers nums, write a function maxSubarraySum to find the maximum sum of a contiguous subarray within the array and return that maximum sum. The subarray must be contiguous, meaning that the elements must appear consecutively in the original array.

Examples

Input: nums = [2, 3, -2, 4] Output: 7 Explanation: Maximum sum is 2 + 3 + (-2) + 4 = 7. Input: nums = [1, -1, -5, -4] Output: 1 Explanation: The maximum sum is 1, which is the single element with the highest value.

Related courses

Course

Machine Learning Engineer Interview Prep

Land your dream machine learning role at Meta, Google, Amazon, Apple, Microsoft, Nvidia, and other top companies. Learn from mock interviews, frameworks, and advice from senior candidates. Explore ML system design, core concepts, coding, behavioral interviews, and more.

Course

Data Science Interview Prep

Land your dream data science role at Google, Amazon, Microsoft, Meta, Apple, and other top companies. Learn from mock interviews, frameworks, and advice from senior candidates. Practice statistics, experimentation, coding, SQL, machine learning, behavioral interviews, and more.