Advertisement

Maximum Subarray - LeetCode 53 Solution

Maximum Subarray - Complete Solution Guide

Maximum Subarray is LeetCode problem 53, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

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 <= 10 5 -10 4 <= nums[i] <= 10 4 Follow up: If you have

Detailed Explanation

The problem asks us to find the contiguous subarray (a sequence of consecutive elements) within a given integer array that has the largest sum. We need to return only the sum of this maximum subarray, not the subarray itself. The input array can contain positive, negative, and zero values. The constraint specifies that the array will always have at least one element.

Solution Approach

The provided code implements Kadane's Algorithm. It iterates through the array, maintaining two variables: `current_max` and `max_so_far`. `current_max` stores the maximum sum of a subarray ending at the current position. `max_so_far` stores the overall maximum sum encountered so far. At each step, we decide whether to extend the current subarray by including the current element or to start a new subarray from the current element. This decision is based on whether adding the current element to the current subarray sum increases the sum or makes it smaller than the current element itself. The `max_so_far` is updated in each iteration to keep track of the global maximum.

Step-by-Step Algorithm

  1. Step 1: Initialize `max_so_far` and `current_max` to the first element of the array.
  2. Step 2: Iterate through the array from the second element.
  3. Step 3: For each element, update `current_max` as the maximum between the current element itself and the sum of the current element and the previous `current_max`. This determines whether to start a new subarray or extend the existing one.
  4. Step 4: Update `max_so_far` as the maximum between `max_so_far` and `current_max`. This ensures that `max_so_far` always holds the largest subarray sum seen so far.
  5. Step 5: After the loop completes, return `max_so_far`.

Key Insights

  • Insight 1: A crucial observation is that if the current subarray sum becomes negative, it's better to start a new subarray from the next element, as adding a negative sum to any subsequent elements will only decrease the overall sum.
  • Insight 2: Dynamic Programming can be used effectively. We keep track of the 'current maximum' sum ending at each element and the 'overall maximum' sum seen so far.
  • Insight 3: The problem can be solved in O(n) time with O(1) space by using Kadane's Algorithm, which is a specific application of dynamic programming for this type of problem.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(1)

Topics

This problem involves: Array, Divide and Conquer, Dynamic Programming.

Companies

Asked at: Accenture, Adobe, Amazon, Apple, Atlassian, Autodesk, Barclays, Bloomberg, Cisco, Cognizant, DE Shaw, Goldman Sachs, Google, HashedIn, Huawei, IBM, Infosys, Intel, J.P. Morgan, LinkedIn, Meta, Microsoft, Morgan Stanley, Nike, Nvidia, Optum, Oracle, PayPal, PornHub, Qualcomm, Ripple, Rippling, SAP, Samsung, ServiceNow, Snap, Target, Tekion, Tesla, TikTok, Turing, Uber, Upstart, Vimeo, Visa, Walmart Labs, Wix, Yahoo, Zoho, Zomato, eBay, persistent systems, tcs.