Update global max if current subarray sum is better
OutputSuccess
Maximum subarray sum = 6
Complexity Analysis
Time: O(n) because we scan the array once updating sums
Space: O(1) because we only keep track of two variables
vs Alternative: Naive approach checks all subarrays with O(n^2) or O(n^3) time, Kadane's is much faster
Edge Cases
empty array
returns 0 as no subarray exists
DSA Rust
if nums.is_empty() { return0; }
all negative numbers
returns the largest (least negative) single element
DSA Rust
let mut max_ending_here = nums[0];
single element array
returns that element as max sum
DSA Rust
let mut max_ending_here = nums[0];
When to Use This Pattern
When asked to find the maximum sum of a continuous subarray, reach for Kadane's algorithm because it efficiently tracks local and global sums in one pass.
Common Mistakes
Mistake: Resetting max_ending_here to zero instead of current element when sum drops below zero Fix: Reset max_ending_here to current element to handle negative numbers correctly
Summary
Finds the maximum sum of any continuous subarray in a list.
Use when you need the largest sum of consecutive elements efficiently.
Keep track of the best sum ending at each position to avoid checking all subarrays.
Practice
(1/5)
1. What does Kadane's Algorithm primarily find in an array of integers?
easy
A. The maximum sum of a contiguous subarray
B. The minimum element in the array
C. The total sum of all elements
D. The maximum product of any two elements
Solution
Step 1: Understand the problem Kadane's Algorithm solves
Kadane's Algorithm is designed to find the maximum sum of a contiguous subarray within a one-dimensional array of numbers.
Step 2: Differentiate from other array operations
It does not find minimum elements, total sums, or products, but specifically the maximum sum of a continuous segment.
Final Answer:
The maximum sum of a contiguous subarray -> Option A
Quick Check:
Maximum subarray sum = maximum sum of contiguous elements [OK]
Hint: Kadane's finds max sum in a continuous slice [OK]
Common Mistakes:
Confusing maximum sum with maximum product
Thinking it finds minimum elements
Assuming it sums all elements
2. Which of the following Rust code snippets correctly initializes variables for Kadane's Algorithm?
easy
A. let mut max_so_far = 0; let max_ending_here = i32::MIN;
B. let max_so_far = 0; let max_ending_here = 0;
C. let mut max_so_far = i32::MIN; let mut max_ending_here = 0;
D. let max_so_far = i32::MAX; let max_ending_here = 0;
Solution
Step 1: Identify correct initialization for Kadane's variables
max_so_far should start at the smallest possible integer (i32::MIN) to handle all negative arrays; max_ending_here starts at 0 to accumulate sums.
Step 2: Check other options for correctness
Options B and C incorrectly initialize variables, and D uses i32::MAX which is incorrect for max_so_far.
Final Answer:
let mut max_so_far = i32::MIN; let mut max_ending_here = 0; -> Option C
Quick Check:
Initialize max_so_far to smallest int, max_ending_here to 0 [OK]
Hint: Start max_so_far at i32::MIN, max_ending_here at 0 [OK]
Common Mistakes:
Initializing max_so_far to 0 causes errors with all negative arrays
Using i32::MAX for max_so_far is wrong
Not using mutable variables for updates
3. What is the output of the following Rust code using Kadane's Algorithm?
let nums = vec![4, -1, 2, 1, -5, 4];
let mut max_so_far = i32::MIN;
let mut max_ending_here = 0;
for &x in &nums {
max_ending_here = (max_ending_here + x).max(x);
max_so_far = max_so_far.max(max_ending_here);
}
println!("{}", max_so_far);
medium
A. 5
B. 7
C. 4
D. 6
Solution
Step 1: Trace max_ending_here and max_so_far through iterations