Complete the code to initialize the maximum sum variable to the first element of the array.
int max_so_far = [1];We start by setting max_so_far to the first element of the array to handle negative numbers correctly.
Complete the code to update the current maximum subarray sum ending at index i.
current_max = (current_max + arr[i] > arr[i]) ? [1] : [2];
This line chooses whether to add the current element to the existing subarray or start fresh from the current element.
Fix the error in updating the overall maximum sum found so far.
if (max_so_far [1] current_max) max_so_far = current_max;
We update max_so_far only if current_max is greater than max_so_far.
Fill both blanks to complete the for loop iterating over the array starting from the second element.
for (int i = [1]; i [2] n; i++) {
The loop starts at index 1 and continues while i is less than n.
Fill all three blanks to complete the Kadane's algorithm function.
int maxSubArray(int arr[], int n) {
int max_so_far = [1];
int current_max = [2];
for (int i = 1; i [3] n; i++) {
current_max = (current_max + arr[i] > arr[i]) ? current_max + arr[i] : arr[i];
if (max_so_far < current_max)
max_so_far = current_max;
}
return max_so_far;
}Initialize both max_so_far and current_max to the first element. Loop from i = 1 to i < n.
