Bird
0
0
DSA Cprogramming~10 mins

Maximum Product 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 product variable.

DSA C
int maxProduct(int* nums, int numsSize) {
    int maxProd = [1];
    return maxProd;
}
Drag options to blanks, or click blank then click option'
A1
B0
C-1
Dnums[0]
Attempts:
3 left
💡 Hint
Common Mistakes
Initializing maxProd to 0 or 1 instead of nums[0].
2fill in blank
medium

Complete the code to update the maximum product during iteration.

DSA C
for (int i = 1; i < numsSize; i++) {
    maxProd = (maxProd > nums[i]) ? maxProd : [1];
}
Drag options to blanks, or click blank then click option'
Anums[i]
BmaxProd * nums[i]
Cnums[i] * nums[i]
DmaxProd + nums[i]
Attempts:
3 left
💡 Hint
Common Mistakes
Using multiplication or addition instead of direct comparison.
3fill in blank
hard

Fix the error in the code to correctly track the minimum product for negative numbers.

DSA C
int temp = maxProd;
maxProd = (maxProd > 0) ? maxProd * nums[i] : [1];
minProd = (minProd < 0) ? minProd * nums[i] : nums[i];
Drag options to blanks, or click blank then click option'
Atemp * nums[i]
BminProd * nums[i]
Cnums[i]
DmaxProd * nums[i]
Attempts:
3 left
💡 Hint
Common Mistakes
Using minProd or nums[i] instead of the stored maxProd value.
4fill in blank
hard

Fill both blanks to update maxProd and minProd correctly when nums[i] is negative.

DSA C
if (nums[i] < 0) {
    int temp = [1];
    [2] = maxProd;
    maxProd = minProd * nums[i];
    minProd = temp * nums[i];
}
Drag options to blanks, or click blank then click option'
AmaxProd
BminProd
Cnums[i]
Dtemp
Attempts:
3 left
💡 Hint
Common Mistakes
Not swapping maxProd and minProd correctly when nums[i] is negative.
5fill in blank
hard

Fill all three blanks to complete the maximum product subarray function.

DSA C
int maxProduct(int* nums, int numsSize) {
    int maxProd = nums[0], minProd = nums[0], result = nums[0];
    for (int i = 1; i < numsSize; i++) {
        if (nums[i] < 0) {
            int temp = [1];
            [2] = maxProd;
            maxProd = minProd * nums[i];
            minProd = temp * nums[i];
        } else {
            maxProd = (maxProd > 0) ? maxProd * nums[i] : nums[i];
            minProd = (minProd < 0) ? minProd * nums[i] : nums[i];
        }
        if (maxProd > result) {
            result = maxProd;
        }
    }
    return [3];
}
Drag options to blanks, or click blank then click option'
AmaxProd
BminProd
Cresult
Dnums[i]
Attempts:
3 left
💡 Hint
Common Mistakes
Returning maxProd or minProd instead of result.
Not swapping maxProd and minProd correctly.