Challenge - 5 Problems
Kadane's Algorithm Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output of Kadane's Algorithm on this array?
Given the array
int[] arr = {-2, 1, -3, 4, -1, 2, 1, -5, 4};, what is the maximum subarray sum found by Kadane's Algorithm?DSA C#
int[] arr = {-2, 1, -3, 4, -1, 2, 1, -5, 4}; int maxSoFar = arr[0]; int maxEndingHere = arr[0]; for (int i = 1; i < arr.Length; i++) { maxEndingHere = Math.Max(arr[i], maxEndingHere + arr[i]); maxSoFar = Math.Max(maxSoFar, maxEndingHere); } Console.WriteLine(maxSoFar);
Attempts:
2 left
💡 Hint
Look for the contiguous subarray with the largest sum.
✗ Incorrect
The maximum subarray is [4, -1, 2, 1] which sums to 6.
❓ Predict Output
intermediateWhat is the maximum subarray sum for all negative numbers?
Given the array
int[] arr = {-8, -3, -6, -2, -5, -4};, what will Kadane's Algorithm output as the maximum subarray sum?DSA C#
int[] arr = {-8, -3, -6, -2, -5, -4}; int maxSoFar = arr[0]; int maxEndingHere = arr[0]; for (int i = 1; i < arr.Length; i++) { maxEndingHere = Math.Max(arr[i], maxEndingHere + arr[i]); maxSoFar = Math.Max(maxSoFar, maxEndingHere); } Console.WriteLine(maxSoFar);
Attempts:
2 left
💡 Hint
Kadane's Algorithm returns the largest single element if all are negative.
✗ Incorrect
The largest number in the array is -2, so the maximum subarray sum is -2.
🔧 Debug
advancedWhat error occurs in this Kadane's Algorithm code?
Consider this code snippet for Kadane's Algorithm. What error will it cause when run?
int[] arr = {1, -2, 3};
int maxSoFar = 0;
int maxEndingHere = 0;
for (int i = 1; i < arr.Length; i++) {
maxEndingHere = Math.Max(arr[i], maxEndingHere + arr[i]);
maxSoFar = Math.Max(maxSoFar, maxEndingHere);
}
Console.WriteLine(maxSoFar);DSA C#
int[] arr = {1, -2, 3}; int maxSoFar = 0; int maxEndingHere = 0; for (int i = 1; i <= arr.Length; i++) { maxEndingHere = Math.Max(arr[i], maxEndingHere + arr[i]); maxSoFar = Math.Max(maxSoFar, maxEndingHere); } Console.WriteLine(maxSoFar);
Attempts:
2 left
💡 Hint
Check the loop boundary conditions carefully.
✗ Incorrect
The loop uses i <= arr.Length which tries to access arr[arr.Length], causing IndexOutOfRangeException.
🧠 Conceptual
advancedWhy does Kadane's Algorithm reset the current sum?
In Kadane's Algorithm, why do we choose the maximum between the current element and the sum of current element plus previous sum?
Attempts:
2 left
💡 Hint
Think about when continuing the previous subarray is worse than starting new.
✗ Incorrect
If the previous sum is negative, starting fresh from current element gives a better sum.
🚀 Application
expertFind the maximum subarray sum and its indices
Modify Kadane's Algorithm to find not only the maximum subarray sum but also the start and end indices of that subarray in
int[] arr = { -1, 4, -2, 3, -2, 3, -5, 4 };. What are the correct start and end indices of the maximum subarray?DSA C#
int[] arr = { -1, 4, -2, 3, -2, 3, -5, 4 }; int maxSoFar = arr[0]; int maxEndingHere = arr[0]; int start = 0, end = 0, s = 0; for (int i = 1; i < arr.Length; i++) { if (arr[i] > maxEndingHere + arr[i]) { maxEndingHere = arr[i]; s = i; } else { maxEndingHere += arr[i]; } if (maxEndingHere > maxSoFar) { maxSoFar = maxEndingHere; start = s; end = i; } } Console.WriteLine($"Max sum: {maxSoFar}, Start: {start}, End: {end}");
Attempts:
2 left
💡 Hint
Track the start index when resetting the current sum.
✗ Incorrect
The maximum subarray is [4, -2, 3, -2, 3] from index 1 to 5 with sum 6.
