Challenge - 5 Problems
Maximum Product Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Maximum Product Subarray Calculation
What is the output of the following C++ code that finds the maximum product of a contiguous subarray?
DSA C++
int maxProduct(vector<int>& nums) { int maxProd = nums[0], minProd = nums[0], result = nums[0]; for (int i = 1; i < nums.size(); i++) { if (nums[i] < 0) std::swap(maxProd, minProd); maxProd = max(nums[i], maxProd * nums[i]); minProd = min(nums[i], minProd * nums[i]); result = max(result, maxProd); } return result; } int main() { vector<int> nums = {2, 3, -2, 4}; cout << maxProduct(nums); return 0; }
Attempts:
2 left
💡 Hint
Think about how negative numbers affect the product and why swapping max and min helps.
✗ Incorrect
The maximum product subarray is [2, 3] with product 6. The negative number -2 resets the product, and 4 alone is less than 6.
❓ Predict Output
intermediateResult of Maximum Product Subarray with Negative Numbers
What will be printed by this C++ code for the maximum product subarray?
DSA C++
int maxProduct(vector<int>& nums) { int maxProd = nums[0], minProd = nums[0], result = nums[0]; for (int i = 1; i < nums.size(); i++) { if (nums[i] < 0) std::swap(maxProd, minProd); maxProd = max(nums[i], maxProd * nums[i]); minProd = min(nums[i], minProd * nums[i]); result = max(result, maxProd); } return result; } int main() { vector<int> nums = {-2, 0, -1}; cout << maxProduct(nums); return 0; }
Attempts:
2 left
💡 Hint
Zero resets the product, so consider subarrays around zero.
✗ Incorrect
The maximum product subarray is [0] with product 0. Negative numbers alone are less than zero.
🔧 Debug
advancedIdentify the Error in Maximum Product Subarray Code
This code aims to find the maximum product subarray but produces wrong output. What is the error?
DSA C++
int maxProduct(vector<int>& nums) { int maxProd = 1, minProd = 1, result = INT_MIN; for (int i = 0; i < nums.size(); i++) { if (nums[i] < 0) std::swap(maxProd, minProd); maxProd = max(nums[i], maxProd * nums[i]); minProd = min(nums[i], minProd * nums[i]); result = max(result, maxProd); } return result; }
Attempts:
2 left
💡 Hint
Initialization affects the first iteration's calculations.
✗ Incorrect
Starting maxProd and minProd at 1 ignores the first element's value, causing incorrect results.
🧠 Conceptual
advancedWhy Swap maxProd and minProd When Encountering Negative Number?
In the maximum product subarray algorithm, why do we swap maxProd and minProd when the current number is negative?
Attempts:
2 left
💡 Hint
Think about how a negative number changes the sign of a product.
✗ Incorrect
Multiplying by a negative number turns the largest product into the smallest and vice versa, so swapping helps track both correctly.
🚀 Application
expertMaximum Product Subarray Result for Complex Input
Given the input array, what is the maximum product of any contiguous subarray?
DSA C++
vector<int> nums = {1, -2, -3, 0, 7, -8, -2};
Attempts:
2 left
💡 Hint
Consider how negative numbers multiply to positive and how zero resets the product.
✗ Incorrect
The maximum product subarray is [7, -8, -2] with product 112.
