Maximum Product After K Increments - Problem

You are given an array of non-negative integers nums and an integer k. In one operation, you may choose any element from nums and increment it by 1.

Return the maximum product of nums after at most k operations. Since the answer may be very large, return it modulo 109 + 7.

Note that you should maximize the product before taking the modulo.

Input & Output

Example 1 — Basic Case
$ Input: nums = [0,4], k = 5
Output: 20
💡 Note: Increment the first element 5 times: [0,4] → [5,4]. Product = 5 × 4 = 20.
Example 2 — Multiple Small Elements
$ Input: nums = [6,3,3,2], k = 2
Output: 216
💡 Note: Increment smallest elements: [6,3,3,2] → [6,3,3,3] → [6,3,4,3]. Product = 6 × 3 × 4 × 3 = 216.
Example 3 — Zero in Array
$ Input: nums = [1,0,1], k = 1
Output: 1
💡 Note: Increment the zero: [1,0,1] → [1,1,1]. Product = 1 × 1 × 1 = 1.

Constraints

  • 1 ≤ nums.length ≤ 105
  • 0 ≤ nums[i] ≤ 106
  • 1 ≤ k ≤ 106

Visualization

Tap to expand
Maximum Product After K IncrementsInput: nums = [0, 4], k = 504Strategy: Increment smallest element (0) five times+5 increments54Maximum Product: 5 × 4 = 20
Understanding the Visualization
1
Input
Array [0,4] with k=5 increments available
2
Strategy
Always increment smallest element for maximum product impact
3
Output
Final array [5,4] gives maximum product of 20
Key Takeaway
🎯 Key Insight: Always increment the smallest numbers first to maximize the multiplicative effect on the final product
Asked in
Google 12 Amazon 8 Microsoft 6 Apple 4
12.5K Views
Medium Frequency
~25 min Avg. Time
425 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