Maximum Subarray Solution for Accenture

Company: Accenture · Difficulty: Medium · Topics: Array, Dynamic Programming, Divide and Conquer

Problem Statement

Given an integer array nums, find the subarray with the largest sum, and return its sum.

Key Understanding:

Examples

Example 1:

Input: nums = [-2,1,-3,4,-1,2,1,-5,4]

Output: 6

Explanation:

Example 2:

Input: nums = [1]

Output: 1

Explanation:

Example 3:

Input: nums = [5,4,-1,7,8]

Output: 23

Explanation:

Example 4:

Input: nums = [-1,-2,-3,-4]

Output: -1

Explanation:

Intuition

The key insight is that we can use dynamic programming to keep track of the maximum sum ending at each position. If the current sum becomes negative, we start a new subarray from the current element.

Core Concept:

Why This Works:

Approach

Approach 1: Kadane's Algorithm (Optimal)

Algorithm:

1. Initialize maxSum and currentSum to the first element

2. Iterate from the second element to the end

3. For each element:

4. Return maxSum

Why This Works:

Complexity Analysis:

Approach 2: Divide and Conquer

Algorithm:

1. Divide the array into two halves

2. Recursively find the maximum subarray in the left half

3. Recursively find the maximum subarray in the right half

4. Find the maximum subarray that crosses the middle

5. Return the maximum of the three values

Why This Works:

Complexity Analysis:

Approach 3: Brute Force

Algorithm:

1. Check all possible subarrays

2. Calculate the sum for each subarray

3. Keep track of the maximum sum

Why This Works:

Complexity Analysis:

Dry Run

Let's trace through Example 1: nums = [-2,1,-3,4,-1,2,1,-5,4]

Kadane's Algorithm:

Initial State: maxSum = -2, currentSum = -2

Step 1: i = 1, num = 1

Step 2: i = 2, num = -3

Step 3: i = 3, num = 4

Step 4: i = 4, num = -1

Step 5: i = 5, num = 2

Step 6: i = 6, num = 1

Step 7: i = 7, num = -5

Step 8: i = 8, num = 4

Final Result: 6

Divide and Conquer Approach:

Array: [-2,1,-3,4,-1,2,1,-5,4]

Divide: Left = [-2,1,-3,4], Right = [-1,2,1,-5,4]

Left Half: [-2,1,-3,4]

Right Half: [-1,2,1,-5,4]

Crossing Middle: Find max subarray that includes elements from both halves

Final Result: max(4, 6, 10) = 10

Key Insights

1. Dynamic Programming: Kadane's algorithm is a classic dynamic programming problem

2. Local vs Global Maximum: We maintain both local maximum (currentSum) and global maximum (maxSum)

3. Negative Sums: If current sum becomes negative, it's better to start fresh

4. Contiguous Subarray: The subarray must be contiguous (no gaps)

5. Single Element: A single element can be the maximum subarray

Edge Cases

1. All Negative Numbers: Maximum subarray is the largest single element

2. All Positive Numbers: Maximum subarray is the entire array

3. Single Element: The element itself is the maximum subarray

4. Empty Array: Not possible as per constraints (1 ≤ nums.length ≤ 10^5)

5. Large Numbers: Need to handle integer overflow

Solution

⚠️ Disclaimer: Don't jump directly to the solution, try it out yourself first. Only look at the solution after you've given it a good attempt.

Solution 1: Kadane's Algorithm (Optimal)

Time Complexity: O(n) - We traverse the array once

Space Complexity: O(1) - We use only constant extra space

Advantages:

Disadvantages:

Solution 2: Divide and Conquer

Time Complexity: O(n log n) - We divide the problem recursively

Space Complexity: O(log n) - Due to recursion stack

Advantages:

Disadvantages:

Solution 3: Brute Force

Time Complexity: O(n²) - We check n(n+1)/2 subarrays

Space Complexity: O(1) - We use only constant extra space

Advantages:

Disadvantages:

More Accenture coding questions