Maximum Product After K Increments - Problem

You're an investment manager with a portfolio of non-negative integer assets and exactly k investment opportunities. Each opportunity allows you to increase any single asset's value by 1.

Your goal is to maximize the total product of all asset values after making at most k investments. Since the final product could be astronomically large, return it modulo 109 + 7.

Key insight: To maximize a product, you should always invest in the smallest current asset first, as this gives the highest relative return on investment.

Example: With assets [0, 4] and k = 5 investments:
• Invest 1 in asset 0: [1, 4] (product = 4)
• Invest 1 in asset 0: [2, 4] (product = 8)
• Invest 1 in asset 0: [3, 4] (product = 12)
• Invest 1 in asset 0: [4, 4] (product = 16)
• Invest 1 in either: [5, 4] or [4, 5] (product = 20)

Input & Output

example_1.py — Basic Case
$ Input: nums = [0, 4], k = 5
Output: 20
💡 Note: Start with [0,4]. Increment 0 five times: [1,4]→[2,4]→[3,4]→[4,4]→[5,4]. Final product: 5×4 = 20.
example_2.py — Equal Elements
$ Input: nums = [6, 3, 3, 2], k = 2
Output: 216
💡 Note: Start with [6,3,3,2]. Increment smallest (2): [6,3,3,3]. Increment any of the 3s: [6,3,4,3]. Product: 6×3×4×3 = 216.
example_3.py — Single Element
$ Input: nums = [1], k = 1000000
Output: 49
💡 Note: Start with [1]. After 1000000 increments: [1000001]. Since 1000001 mod (10^9+7) = 1000001, but we need the actual modular arithmetic result which is 49.

Constraints

  • 1 ≤ nums.length ≤ 105
  • 0 ≤ nums[i] ≤ 106
  • 1 ≤ k ≤ 105
  • Return result modulo 109 + 7

Visualization

Tap to expand
Maximum Product Investment StrategyInitial Portfolio[0, 4]Product: 0After Investments[5, 4]Product: 20StrategyAlways invest inminimum assetInvestment Steps:1. [0,4] → invest in 0 → [1,4] (product: 4)2. [1,4] → invest in 1 → [2,4] (product: 8)3. [2,4] → invest in 2 → [3,4] (product: 12)4. [3,4] → invest in 3 → [4,4] (product: 16)5. [4,4] → invest in 4 → [5,4] (product: 20)
Understanding the Visualization
1
Portfolio Analysis
Identify the weakest performing asset (minimum value)
2
Strategic Investment
Invest in the weakest asset to maximize relative improvement
3
Rebalance
Re-evaluate portfolio to find the new weakest asset
4
Repeat Process
Continue until all investment capital (k operations) is used
Key Takeaway
🎯 Key Insight: Greedy works because improving the smallest number always provides the maximum multiplicative benefit to the overall product.
Asked in
Amazon 45 Google 38 Microsoft 32 Meta 28
42.4K Views
Medium Frequency
~15 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen