Kadane's Algorithm Maximum Subarray in DSA C - Time & Space Complexity
We want to understand how the time taken by Kadane's Algorithm changes as the input array grows.
Specifically, how many steps does it take to find the maximum sum of a continuous subarray?
Analyze the time complexity of the following code snippet.
int maxSubArray(int* nums, int numsSize) {
int maxSoFar = nums[0];
int currentMax = nums[0];
for (int i = 1; i < numsSize; i++) {
currentMax = (nums[i] > currentMax + nums[i]) ? nums[i] : currentMax + nums[i];
maxSoFar = (maxSoFar > currentMax) ? maxSoFar : currentMax;
}
return maxSoFar;
}
This code finds the largest sum of any continuous subarray within the input array.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: A single for-loop that goes through each element once.
- How many times: Exactly once for each element in the array, so n times.
As the array size grows, the number of steps grows directly with it.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 steps |
| 100 | About 100 steps |
| 1000 | About 1000 steps |
Pattern observation: The steps increase in a straight line as input size increases.
Time Complexity: O(n)
This means the time taken grows directly in proportion to the size of the input array.
[X] Wrong: "Kadane's Algorithm checks all subarrays, so it must be O(n²) or worse."
[OK] Correct: Kadane's cleverly keeps track of sums as it goes, so it only needs one pass through the array, not checking every subarray separately.
Understanding Kadane's Algorithm time complexity shows you can spot efficient solutions that avoid unnecessary work, a key skill in coding interviews.
"What if we changed the algorithm to check every possible subarray sum by nested loops? How would the time complexity change?"
Practice
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: Compare with other options
Options B, C, and D describe different problems unrelated to Kadane's Algorithm.Final Answer:
The maximum sum of a contiguous subarray -> Option CQuick Check:
Maximum subarray sum = maximum sum of contiguous elements [OK]
- Confusing max sum with max product
- Thinking it finds minimum element
- Assuming it sums all elements
Solution
Step 1: Understand initialization in Kadane's Algorithm
We initialize max_so_far to the smallest integer (INT_MIN) to handle arrays with all negative numbers correctly, and max_ending_here to 0 to start accumulating sums.Step 2: Evaluate each option
int max_so_far = 0, max_ending_here = 0; initializes max_so_far to 0, which fails for all negative arrays. int max_so_far = 0, max_ending_here = INT_MIN; incorrectly initializes max_ending_here to INT_MIN. int max_so_far = NULL, max_ending_here = 0; uses NULL which is invalid for int.Final Answer:
int max_so_far = INT_MIN, max_ending_here = 0; -> Option AQuick Check:
Use INT_MIN for max_so_far to handle negatives [OK]
- Initializing max_so_far to 0 causes wrong results
- Using NULL for integer variables
- Swapping initial values of variables
int arr[] = {-2, 3, 2, -1};
int n = 4;
int max_so_far = INT_MIN, max_ending_here = 0;
for (int i = 0; i < n; i++) {
max_ending_here += arr[i];
if (max_so_far < max_ending_here) max_so_far = max_ending_here;
if (max_ending_here < 0) max_ending_here = 0;
}
printf("%d", max_so_far);Solution
Step 1: Trace the loop and update sums
Iteration 0: max_ending_here = 0 + (-2) = -2; max_so_far = max(INT_MIN, -2) = -2; max_ending_here < 0, reset to 0.
Iteration 1: max_ending_here = 0 + 3 = 3; max_so_far = max(-2, 3) = 3; max_ending_here >= 0, no reset.
Iteration 2: max_ending_here = 3 + 2 = 5; max_so_far = max(3, 5) = 5; max_ending_here >= 0, no reset.
Iteration 3: max_ending_here = 5 + (-1) = 4; max_so_far = max(5, 4) = 5; max_ending_here >= 0, no reset.Step 2: Final max_so_far value
After loop ends, max_so_far is 5, which is the maximum subarray sum.Final Answer:
5 -> Option BQuick Check:
Max subarray sum = 5 [OK]
- Not resetting max_ending_here when negative
- Confusing max_so_far with current sum
- Incorrectly summing non-contiguous elements
int maxSubArraySum(int arr[], int n) {
int max_so_far = 0, max_ending_here = 0;
for (int i = 0; i < n; i++) {
max_ending_here += arr[i];
if (max_so_far < max_ending_here) max_so_far = max_ending_here;
if (max_ending_here < 0) max_ending_here = 0;
}
return max_so_far;
}Solution
Step 1: Analyze initialization of max_so_far
Initializing max_so_far to 0 fails when all array elements are negative, as max_so_far will never update to a negative number.Step 2: Correct initialization and effect
Setting max_so_far to INT_MIN ensures the algorithm correctly finds the maximum subarray sum even if all numbers are negative.Final Answer:
max_so_far should be initialized to INT_MIN -> Option AQuick Check:
Use INT_MIN for max_so_far to handle negative arrays [OK]
- Initializing max_so_far to 0 causes wrong max for negatives
- Changing loop start index unnecessarily
- Returning wrong variable
int arr[] = {-3, -2, -1, -4}, what is the maximum subarray sum using Kadane's Algorithm?Solution
Step 1: Understand Kadane's behavior with all negative numbers
When all numbers are negative, resetting max_ending_here to 0 loses the maximum negative number. Proper Kadane's initialization with max_so_far = INT_MIN helps track the largest negative number.Step 2: Trace the array values
max_so_far starts at INT_MIN.
i=0: max_ending_here = -3; max_so_far = -3; reset max_ending_here to 0 since negative.
i=1: max_ending_here = -2; max_so_far = max(-3, -2) = -2; reset max_ending_here to 0.
i=2: max_ending_here = -1; max_so_far = max(-2, -1) = -1; reset max_ending_here to 0.
i=3: max_ending_here = -4; max_so_far = max(-1, -4) = -1; reset max_ending_here to 0.Final Answer:
-1 -> Option DQuick Check:
Max negative number = -1 [OK]
- Returning 0 instead of max negative
- Not handling all negative arrays
- Assuming sum must be positive
Ready to prove it?
Pass the test and this pattern moves unknown
