📖 Scenario: Imagine you are analyzing daily sales growth rates of a product over a week. Each day's growth rate can be positive (growth), negative (decline), or zero (no change). You want to find the period (contiguous days) where the product of growth rates is the highest, indicating the best continuous growth streak.
🎯 Goal: Build a Go program that finds the maximum product of any contiguous subarray within a given list of integers representing daily growth rates.
📋 What You'll Learn
Create a slice of integers named growthRates with the exact values: 2, 3, -2, 4
Create two integer variables maxProduct and minProduct initialized to the first element of growthRates
Use a for loop with index i starting from 1 to iterate over growthRates
Inside the loop, update maxProduct and minProduct to track the maximum and minimum products ending at current index
Create an integer variable result initialized to the first element of growthRates to store the maximum product found
Print the value of result at the end
💡 Why This Matters
🌍 Real World
Finding the maximum product subarray helps in analyzing periods of best continuous growth or decline in financial data, sales, or stock prices.
💼 Career
This algorithm is useful for software engineers working in data analysis, finance, or any field requiring time series data processing and optimization.
Progress0 / 4 steps
1
Create the growth rates slice
Create a slice of integers called growthRates with these exact values: 2, 3, -2, 4
DSA Go
Hint
Use growthRates := []int{2, 3, -2, 4} to create the slice.
2
Initialize maxProduct, minProduct, and result
Create three integer variables maxProduct, minProduct, and result all initialized to the first element of growthRates
DSA Go
Hint
Use maxProduct := growthRates[0], minProduct := growthRates[0], and result := growthRates[0].
3
Calculate maximum product subarray
Use a for loop with index i starting from 1 to iterate over growthRates. Inside the loop, create a temporary variable tempMax to store the current maxProduct. Update maxProduct to the maximum of growthRates[i], maxProduct * growthRates[i], and minProduct * growthRates[i]. Update minProduct to the minimum of growthRates[i], tempMax * growthRates[i], and minProduct * growthRates[i]. Update result to the maximum of result and maxProduct.
DSA Go
Hint
Remember to create helper functions max and min to compare two integers.
4
Print the maximum product
Print the value of result using fmt.Println(result)
DSA Go
Hint
Use fmt.Println(result) to print the maximum product.
Practice
(1/5)
1. What does the "Maximum Product Subarray" problem ask you to find in an array of integers?
easy
A. The longest subarray with positive numbers
B. The contiguous subarray with the largest sum of its elements
C. The maximum single element in the array
D. The contiguous subarray with the largest product of its elements
Solution
Step 1: Understand the problem statement
The problem asks for a contiguous part of the array where the product of all elements is the highest possible.
Step 2: Differentiate from similar problems
Unlike maximum sum subarray, here we multiply elements, so negative numbers and zeros affect the product differently.
Final Answer:
The contiguous subarray with the largest product of its elements -> Option D
Quick Check:
Maximum product subarray = contiguous subarray with max product [OK]
Hint: Look for contiguous elements with max multiplication [OK]
Common Mistakes:
Confusing product with sum
Ignoring negative numbers impact
Thinking about non-contiguous elements
2. Which of the following Go code snippets correctly initializes variables to track the maximum product subarray?
easy
A. maxProd, minProd, result := 1, 1, 1
B. maxProd, minProd, result := 0, 0, 0
C. maxProd, minProd, result := nums[0], nums[0], nums[0]
D. maxProd, minProd, result := -1, -1, -1
Solution
Step 1: Understand initialization for max product subarray
We start with the first element as initial max, min, and result to handle negative and positive values correctly.
Step 2: Check each option's initialization
maxProd, minProd, result := nums[0], nums[0], nums[0] uses nums[0] which is correct; others use fixed values that may not match array's first element.
Final Answer:
maxProd, minProd, result := nums[0], nums[0], nums[0] -> Option C
Quick Check:
Initialize with first element for correct tracking [OK]
Hint: Initialize with first element, not zero or one [OK]
Common Mistakes:
Initializing with zero causes wrong results if nums[0] is negative
Using 1 ignores negative or zero first elements
Not initializing minProd separately
3. What is the output of the following Go code for the input array nums := []int{2, 3, -2, 4}?
func maxProduct(nums []int) int {
maxProd, minProd, result := nums[0], nums[0], nums[0]
for i := 1; i < len(nums); i++ {
if nums[i] < 0 {
maxProd, minProd = minProd, maxProd
}
maxProd = max(nums[i], maxProd*nums[i])
minProd = min(nums[i], minProd*nums[i])
if maxProd > result {
result = maxProd
}
}
return result
}
func max(a, b int) int {
if a > b {
return a
}
return b
}
func min(a, b int) int {
if a < b {
return a
}
return b
}
medium
A. 6
B. 4
C. -2
D. 8
Solution
Step 1: Trace the loop and update maxProd, minProd, and result