Challenge - 5 Problems
Kadane's Algorithm Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Kadane's Algorithm on a simple array
What is the output of the following C++ code that uses Kadane's Algorithm to find the maximum subarray sum?
DSA C++
int maxSubArraySum(int arr[], int n) { int max_so_far = arr[0]; int curr_max = arr[0]; for (int i = 1; i < n; i++) { curr_max = std::max(arr[i], curr_max + arr[i]); max_so_far = std::max(max_so_far, curr_max); } return max_so_far; } int main() { int arr[] = {-2, 1, -3, 4, -1, 2, 1, -5, 4}; int n = sizeof(arr) / sizeof(arr[0]); std::cout << maxSubArraySum(arr, n); return 0; }
Attempts:
2 left
💡 Hint
Trace the sum of subarrays and keep track of the maximum sum found so far.
✗ Incorrect
Kadane's algorithm finds the maximum sum of a contiguous subarray. For the given array, the maximum sum subarray is [4, -1, 2, 1] which sums to 6.
❓ Predict Output
intermediateMaximum subarray sum with all negative numbers
What will be the output of the Kadane's Algorithm implementation on the following array?
DSA C++
int maxSubArraySum(int arr[], int n) { int max_so_far = arr[0]; int curr_max = arr[0]; for (int i = 1; i < n; i++) { curr_max = std::max(arr[i], curr_max + arr[i]); max_so_far = std::max(max_so_far, curr_max); } return max_so_far; } int main() { int arr[] = {-8, -3, -6, -2, -5, -4}; int n = sizeof(arr) / sizeof(arr[0]); std::cout << maxSubArraySum(arr, n); return 0; }
Attempts:
2 left
💡 Hint
When all numbers are negative, the maximum subarray is the single largest number.
✗ Incorrect
Kadane's algorithm returns the largest single element when all are negative. Here, -2 is the largest number.
🧠 Conceptual
advancedUnderstanding Kadane's Algorithm variables
In Kadane's Algorithm, what does the variable 'curr_max' represent during the iteration?
Attempts:
2 left
💡 Hint
Think about how the algorithm decides whether to start a new subarray or continue the existing one.
✗ Incorrect
'curr_max' holds the maximum sum of a subarray that ends exactly at the current index. It helps decide whether to extend the previous subarray or start fresh.
❓ Predict Output
advancedOutput of Kadane's Algorithm with zeroes included
What is the output of the following code using Kadane's Algorithm on the given array?
DSA C++
int maxSubArraySum(int arr[], int n) { int max_so_far = arr[0]; int curr_max = arr[0]; for (int i = 1; i < n; i++) { curr_max = std::max(arr[i], curr_max + arr[i]); max_so_far = std::max(max_so_far, curr_max); } return max_so_far; } int main() { int arr[] = {0, -3, 5, -2, 1, 0, -1, 4}; int n = sizeof(arr) / sizeof(arr[0]); std::cout << maxSubArraySum(arr, n); return 0; }
Attempts:
2 left
💡 Hint
Identify the subarray with the largest sum including zeroes.
✗ Incorrect
The maximum subarray is [5, -2, 1, 0, -1, 4] which sums to 7.
🚀 Application
expertFinding maximum subarray sum and its indices
Modify Kadane's Algorithm to also return the start and end indices of the maximum sum subarray. What will be the output for the array below?
DSA C++
std::tuple<int, int, int> maxSubArraySumWithIndices(int arr[], int n) { int max_so_far = arr[0]; int curr_max = arr[0]; int start = 0, end = 0, s = 0; for (int i = 1; i < n; i++) { if (arr[i] > curr_max + arr[i]) { curr_max = arr[i]; s = i; } else { curr_max += arr[i]; } if (curr_max > max_so_far) { max_so_far = curr_max; start = s; end = i; } } return {max_so_far, start, end}; } int main() { int arr[] = {-1, 3, -2, 5, -3, 2}; int n = sizeof(arr) / sizeof(arr[0]); auto [max_sum, start_idx, end_idx] = maxSubArraySumWithIndices(arr, n); std::cout << max_sum << " " << start_idx << " " << end_idx; return 0; }
Attempts:
2 left
💡 Hint
Track the start index when you reset the current sum and update the max sum indices accordingly.
✗ Incorrect
The maximum sum subarray is [3, -2, 5] with sum 6, starting at index 1 and ending at index 3.
