Complete the code to initialize the maximum sum with the first element of the array.
let maxSoFar = [1];We start with the first element as the maximum sum so far because the subarray must contain at least one element.
Complete the code to update the current maximum subarray sum ending at index i.
currentMax = Math.max(nums[i], currentMax [1] nums[i]);
We choose the larger between starting fresh at nums[i] or extending the previous subarray by adding nums[i].
Fix the error in updating the maximum sum found so far.
maxSoFar = Math.max(maxSoFar, [1]);
maxSoFar should be updated with the current maximum subarray sum ending at i, which is currentMax.
Fill both blanks to complete the loop that iterates over the array starting from the second element.
for (let i = [1]; i [2] nums.length; i++) {
The loop starts at index 1 because index 0 is used to initialize maxSoFar and currentMax. The condition uses < to avoid going out of bounds.
Fill all three blanks to complete the Kadane's algorithm function.
function maxSubArray(nums: number[]): number {
let maxSoFar = [1];
let currentMax = [2];
for (let i = 1; i [3] nums.length; i++) {
currentMax = Math.max(nums[i], currentMax + nums[i]);
maxSoFar = Math.max(maxSoFar, currentMax);
}
return maxSoFar;
}Initialize maxSoFar and currentMax with the first element nums[0]. Loop condition uses < to avoid out of bounds.
