Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
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.
Example 1:
Input: nums = [0,4], k = 5 Output: 20 Explanation: Increment the first number 5 times. Now nums = [5, 4], with a product of 5 * 4 = 20. It can be shown that 20 is maximum product possible, so we return 20. Note that there may be other ways to increment nums to have the maximum product.
Example 2:
Input: nums = [6,3,3,2], k = 2 Output: 216 Explanation: Increment the second number 1 time and increment the fourth number 1 time. Now nums = [6, 4, 3, 3], with a product of 6 * 4 * 3 * 3 = 216. It can be shown that 216 is maximum product possible, so we return 216. Note that there may be other ways to increment nums to have the maximum product.
Constraints:
1 <= nums.length, k <= 1050 <= nums[i] <= 106Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Greedy
[0,4] 5
[6,3,3,2] 2
minimum-size-subarray-sum)minimum-increment-to-make-array-unique)minimum-operations-to-make-the-array-increasing)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2233: Maximum Product After K Increments
class Solution {
public int maximumProduct(int[] nums, int k) {
PriorityQueue<Integer> pq = new PriorityQueue<>();
for (int x : nums) {
pq.offer(x);
}
while (k-- > 0) {
pq.offer(pq.poll() + 1);
}
final int mod = (int) 1e9 + 7;
long ans = 1;
for (int x : pq) {
ans = (ans * x) % mod;
}
return (int) ans;
}
}
// Accepted solution for LeetCode #2233: Maximum Product After K Increments
func maximumProduct(nums []int, k int) int {
h := hp{nums}
for heap.Init(&h); k > 0; k-- {
h.IntSlice[0]++
heap.Fix(&h, 0)
}
ans := 1
for _, x := range nums {
ans = (ans * x) % (1e9 + 7)
}
return ans
}
type hp struct{ sort.IntSlice }
func (hp) Push(any) {}
func (hp) Pop() (_ any) { return }
# Accepted solution for LeetCode #2233: Maximum Product After K Increments
class Solution:
def maximumProduct(self, nums: List[int], k: int) -> int:
heapify(nums)
for _ in range(k):
heapreplace(nums, nums[0] + 1)
mod = 10**9 + 7
return reduce(lambda x, y: x * y % mod, nums)
// Accepted solution for LeetCode #2233: Maximum Product After K Increments
/**
* [2233] Maximum Product After K Increments
*
* 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 10^9 + 7. Note that you should maximize the product before taking the modulo.
*
* Example 1:
*
* Input: nums = [0,4], k = 5
* Output: 20
* Explanation: Increment the first number 5 times.
* Now nums = [5, 4], with a product of 5 * 4 = 20.
* It can be shown that 20 is maximum product possible, so we return 20.
* Note that there may be other ways to increment nums to have the maximum product.
*
* Example 2:
*
* Input: nums = [6,3,3,2], k = 2
* Output: 216
* Explanation: Increment the second number 1 time and increment the fourth number 1 time.
* Now nums = [6, 4, 3, 3], with a product of 6 * 4 * 3 * 3 = 216.
* It can be shown that 216 is maximum product possible, so we return 216.
* Note that there may be other ways to increment nums to have the maximum product.
*
*
* Constraints:
*
* 1 <= nums.length, k <= 10^5
* 0 <= nums[i] <= 10^6
*
*/
pub struct Solution {}
// problem: https://leetcode.com/problems/maximum-product-after-k-increments/
// discuss: https://leetcode.com/problems/maximum-product-after-k-increments/discuss/?currentPage=1&orderBy=most_votes&query=
// submission codes start here
impl Solution {
pub fn maximum_product(nums: Vec<i32>, k: i32) -> i32 {
0
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
#[ignore]
fn test_2233_example_1() {
let nums = vec![0, 4];
let k = 5;
let result = 20;
assert_eq!(Solution::maximum_product(nums, k), result);
}
#[test]
#[ignore]
fn test_2233_example_2() {
let nums = vec![6, 3, 3, 2];
let k = 2;
let result = 216;
assert_eq!(Solution::maximum_product(nums, k), result);
}
}
// Accepted solution for LeetCode #2233: Maximum Product After K Increments
function maximumProduct(nums: number[], k: number): number {
const pq = new MinPriorityQueue<number>();
nums.forEach(x => pq.enqueue(x));
while (k--) {
const x = pq.dequeue();
pq.enqueue(x + 1);
}
let ans = 1;
const mod = 10 ** 9 + 7;
while (!pq.isEmpty()) {
ans = (ans * pq.dequeue()) % mod;
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Try every possible combination of choices. With n items each having two states (include/exclude), the search space is 2ⁿ. Evaluating each combination takes O(n), giving O(n × 2ⁿ). The recursion stack or subset storage uses O(n) space.
Greedy algorithms typically sort the input (O(n log n)) then make a single pass (O(n)). The sort dominates. If the input is already sorted or the greedy choice can be computed without sorting, time drops to O(n). Proving greedy correctness (exchange argument) is harder than the implementation.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Locally optimal choices may fail globally.
Usually fails on: Counterexamples appear on crafted input orderings.
Fix: Verify with exchange argument or monotonic objective before committing.