Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the maximum product with the first element of the array.
DSA C#
int maxProduct = [1];Attempts:
3 left
💡 Hint
Common Mistakes
Initializing maxProduct to 0 or 1 which can cause incorrect results if the first element is negative.
✗ Incorrect
We start by setting maxProduct to the first element of the array to handle cases where the array has only one element or all negative numbers.
2fill in blank
mediumComplete the code to update the current maximum product at each step.
DSA C#
maxProduct = Math.Max(maxProduct, [1]);Attempts:
3 left
💡 Hint
Common Mistakes
Using nums[i] directly instead of the current maximum product.
Using currentMin which tracks minimum product, not maximum.
✗ Incorrect
We update maxProduct by comparing it with the current maximum product found at this step.
3fill in blank
hardFix the error in updating currentMax and currentMin when the current number is negative.
DSA C#
if (nums[i] < 0) { int temp = [1]; [2] = currentMin; currentMin = temp; }
Attempts:
3 left
💡 Hint
Common Mistakes
Not swapping currentMax and currentMin causing incorrect product calculations.
Swapping with wrong variables.
✗ Incorrect
When the current number is negative, we swap currentMax and currentMin before updating them to handle sign changes correctly.
4fill in blank
hardFill both blanks to update currentMax and currentMin correctly after handling negative numbers.
DSA C#
currentMax = Math.Max(nums[i], currentMax [1] nums[i]); currentMin = Math.Min(nums[i], currentMin [2] nums[i]);
Attempts:
3 left
💡 Hint
Common Mistakes
Using addition or subtraction instead of multiplication.
Using division which is incorrect here.
✗ Incorrect
We multiply currentMax and currentMin by nums[i] to update the maximum and minimum products ending at the current index.
5fill in blank
hardFill all three blanks to complete the loop that calculates the maximum product subarray.
DSA C#
for (int i = 1; i < nums.Length; i++) { if (nums[i] < 0) { int temp = [1]; [2] = currentMin; currentMin = temp; } currentMax = Math.Max(nums[i], currentMax [3] nums[i]); currentMin = Math.Min(nums[i], currentMin * nums[i]); maxProduct = Math.Max(maxProduct, currentMax); }
Attempts:
3 left
💡 Hint
Common Mistakes
Not swapping currentMax and currentMin correctly.
Using wrong operators in multiplication.
✗ Incorrect
Inside the loop, we swap currentMax and currentMin if nums[i] is negative, then update currentMax and currentMin by multiplying with nums[i].
