Bird
0
0
DSA Cprogramming~10 mins

Kadane's Algorithm Maximum Subarray in DSA C - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to initialize the maximum sum variable to the first element of the array.

DSA C
int max_so_far = [1];
Drag options to blanks, or click blank then click option'
Aarr[0]
B0
CINT_MIN
Darr[1]
Attempts:
3 left
💡 Hint
Common Mistakes
Initializing max_so_far to 0 can cause wrong results if all numbers are negative.
Using arr[1] may cause out-of-bounds errors for single-element arrays.
2fill in blank
medium

Complete the code to update the current maximum subarray sum ending at index i.

DSA C
current_max = (current_max + arr[i] > arr[i]) ? [1] : [2];
Drag options to blanks, or click blank then click option'
Acurrent_max + arr[i]
Barr[i]
Ccurrent_max
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using current_max instead of current_max + arr[i] in the comparison.
Not updating current_max correctly causes wrong maximum subarray sums.
3fill in blank
hard

Fix the error in updating the overall maximum sum found so far.

DSA C
if (max_so_far [1] current_max) max_so_far = current_max;
Drag options to blanks, or click blank then click option'
A>
B<
C==
D<=
Attempts:
3 left
💡 Hint
Common Mistakes
Using > instead of < causes max_so_far to never update correctly.
Using == does not update max_so_far when current_max is greater.
4fill in blank
hard

Fill both blanks to complete the for loop iterating over the array starting from the second element.

DSA C
for (int i = [1]; i [2] n; i++) {
Drag options to blanks, or click blank then click option'
A1
B0
C<
D<=
Attempts:
3 left
💡 Hint
Common Mistakes
Starting from 0 causes double counting the first element.
Using '<=' causes out-of-bounds access.
5fill in blank
hard

Fill all three blanks to complete the Kadane's algorithm function.

DSA C
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;
}
Drag options to blanks, or click blank then click option'
Aarr[0]
C<
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Initializing current_max to 0 causes errors with negative numbers.
Using '<=' in loop causes out-of-bounds errors.