Complete the code to initialize the variable that stores the maximum sum found so far.
int max_so_far = [1];We initialize max_so_far to the smallest integer so any sum will be larger.
Complete the code to update the current sum by adding the current array element.
current_sum = current_sum [1] arr[i];We add the current element to the current sum to extend the subarray.
Fix the error in resetting the current sum when it becomes negative.
if (current_sum [1] 0) { current_sum = 0; }
If the current sum is less than zero, we reset it to zero to start a new subarray.
Fill both blanks to update the maximum sum found so far if the current sum is greater.
if (current_sum [1] max_so_far) { max_so_far [2] current_sum; }
If the current sum is greater than max_so_far, update max_so_far to current_sum.
Fill all three blanks to complete the Kadane's algorithm function that returns the maximum subarray sum.
int maxSubArray(vector<int>& arr) {
int max_so_far = [1];
int current_sum = 0;
for (int i = 0; i < arr.size(); i++) {
current_sum = current_sum [2] arr[i];
if (current_sum > max_so_far) {
max_so_far = current_sum;
}
if (current_sum [3] 0) {
current_sum = 0;
}
}
return max_so_far;
}This function initializes max_so_far to the smallest integer, adds each element to current_sum, resets current_sum if negative, and updates max_so_far when a larger sum is found.
