Challenge - 5 Problems
Kadane's Algorithm Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Kadane's Algorithm on a Mixed Array
What is the output of the following Java code that uses Kadane's algorithm to find the maximum subarray sum?
DSA Java
public class Main { public static int maxSubArray(int[] nums) { int maxSoFar = nums[0]; int maxEndingHere = nums[0]; for (int i = 1; i < nums.length; i++) { maxEndingHere = Math.max(nums[i], maxEndingHere + nums[i]); maxSoFar = Math.max(maxSoFar, maxEndingHere); } return maxSoFar; } public static void main(String[] args) { int[] nums = {-2, 1, -3, 4, -1, 2, 1, -5, 4}; System.out.println(maxSubArray(nums)); } }
Attempts:
2 left
💡 Hint
Think about the largest sum of a continuous subarray.
✗ Incorrect
Kadane's algorithm finds the maximum sum of a contiguous subarray. Here, the maximum sum is 6 from subarray [4, -1, 2, 1].
🧠 Conceptual
intermediateUnderstanding Kadane's Algorithm Variables
In Kadane's algorithm, what does the variable 'maxEndingHere' represent during the iteration?
Attempts:
2 left
💡 Hint
Focus on the subarray that ends exactly at the current position.
✗ Incorrect
'maxEndingHere' keeps track of the maximum sum of a subarray that ends exactly at the current index, helping to decide whether to extend the previous subarray or start fresh.
❓ Predict Output
advancedOutput with All Negative Numbers
What will be the output of the following Java code using Kadane's algorithm on an array with all negative numbers?
DSA Java
public class Main { public static int maxSubArray(int[] nums) { int maxSoFar = nums[0]; int maxEndingHere = nums[0]; for (int i = 1; i < nums.length; i++) { maxEndingHere = Math.max(nums[i], maxEndingHere + nums[i]); maxSoFar = Math.max(maxSoFar, maxEndingHere); } return maxSoFar; } public static void main(String[] args) { int[] nums = {-8, -3, -6, -2, -5, -4}; System.out.println(maxSubArray(nums)); } }
Attempts:
2 left
💡 Hint
Kadane's algorithm returns the largest single element if all are negative.
✗ Incorrect
When all numbers are negative, the maximum subarray is the single largest number, which is -2 here.
🔧 Debug
advancedIdentify the Bug in Kadane's Algorithm Implementation
What error will occur when running this Java code for Kadane's algorithm?
DSA Java
public class Main { public static int maxSubArray(int[] nums) { int maxSoFar = 0; int maxEndingHere = 0; for (int i = 0; i < nums.length; i++) { maxEndingHere = Math.max(nums[i], maxEndingHere + nums[i]); maxSoFar = Math.max(maxSoFar, maxEndingHere); } return maxSoFar; } public static void main(String[] args) { int[] nums = {-1, -2, -3, -4}; System.out.println(maxSubArray(nums)); } }
Attempts:
2 left
💡 Hint
Check initial values of maxSoFar and maxEndingHere for negative arrays.
✗ Incorrect
Initializing maxSoFar and maxEndingHere to 0 causes the algorithm to return 0 even when all numbers are negative, which is incorrect.
🚀 Application
expertMaximum Subarray Sum with Indices
Modify Kadane's algorithm to also return the start and end indices of the maximum subarray. What will be the output for the array [1, -2, 3, 5, -1, 2]?
DSA Java
public class Main { public static int[] maxSubArrayWithIndices(int[] nums) { int maxSoFar = nums[0]; int maxEndingHere = nums[0]; int start = 0, end = 0, s = 0; for (int i = 1; i < nums.length; i++) { if (nums[i] > maxEndingHere + nums[i]) { maxEndingHere = nums[i]; s = i; } else { maxEndingHere += nums[i]; } if (maxEndingHere > maxSoFar) { maxSoFar = maxEndingHere; start = s; end = i; } } return new int[]{maxSoFar, start, end}; } public static void main(String[] args) { int[] nums = {1, -2, 3, 5, -1, 2}; int[] result = maxSubArrayWithIndices(nums); System.out.println("Max Sum: " + result[0] + ", Start: " + result[1] + ", End: " + result[2]); } }
Attempts:
2 left
💡 Hint
Track start index when resetting maxEndingHere.
✗ Incorrect
The maximum subarray is [3, 5, -1, 2] with sum 9, starting at index 2 and ending at index 5.
