53. Maximum Subarray
MediumSolutions in Python, Java, C++, TypeScript
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks us to find the contiguous subarray within a given integer array that has the largest sum. A subarray is a continuous part of an array. For example, in [-2, 1, -3, 4], [1, -3] is a subarray, but [-2, 4] is not. The subarray must contain at least one element.
Solution
Maximum Subarray in Python
class Solution:
def maxSubArray(self, nums: List[int]) -> int:
# According to constraints, nums will have at least one element.
# Initialize current_max and global_max with the first element.
current_max = nums[0]
global_max = nums[0]
# Iterate through the array starting from the second element.
for i in range(1, len(nums)):
num = nums[i]
# The maximum subarray sum ending at the current position is either
# the number itself or the number added to the previous max subarray sum.
current_max = max(num, current_max + num)
# Update the overall global maximum.
global_max = max(global_max, current_max)
return global_max
Maximum Subarray in Java
class Solution {
public int maxSubArray(int[] nums) {
// According to constraints, nums.length is at least 1.
// Initialize currentMax and globalMax with the first element.
int currentMax = nums[0];
int globalMax = nums[0];
// Iterate through the array starting from the second element.
for (int i = 1; i < nums.length; i++) {
// The maximum subarray sum ending at the current position 'i' is
// either the current element nums[i] or the sum of the current element
// and the maximum subarray sum ending at the previous position.
currentMax = Math.max(nums[i], currentMax + nums[i]);
// Update the overall global maximum sum if the current maximum is greater.
globalMax = Math.max(globalMax, currentMax);
}
return globalMax;
}
}
Maximum Subarray in C++
class Solution {
public:
int maxSubArray(vector<int>& nums) {
// According to constraints, nums.length is at least 1.
// Initialize current_max and global_max with the first element.
int current_max = nums[0];
int global_max = nums[0];
// Iterate through the array starting from the second element.
for (int i = 1; i < nums.size(); ++i) {
// The maximum subarray sum ending at the current position 'i' is
// either the current element nums[i] or the sum of the current element
// and the maximum subarray sum ending at the previous position.
current_max = std::max(nums[i], current_max + nums[i]);
// Update the overall global maximum sum if the current maximum is greater.
global_max = std::max(global_max, current_max);
}
return global_max;
}
};
Maximum Subarray in TypeScript
function maxSubArray(nums: number[]): number {
// According to constraints, nums.length is at least 1.
// Initialize current max subarray sum and global max subarray sum
// with the first element of the array.
let currentMax = nums[0];
let globalMax = nums[0];
// Loop through the array starting from the second element.
for (let i = 1; i < nums.length; i++) {
const num = nums[i];
// Calculate the maximum subarray sum ending at the current index.
// It's either the current number itself or the current number
// added to the previous maximum subarray sum.
currentMax = Math.max(num, currentMax + num);
// Update the global maximum sum if the current one is larger.
globalMax = Math.max(globalMax, currentMax);
}
return globalMax;
};
More Medium LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.