Solution: Maximum Product After K Increments

Let’s solve the Maximum Product After K Increments using the Top K Elements pattern.

Statement

You are given an array, nums, consisting of non-negative integers, and an integer k representing the maximum number of allowed operations.

In each operation, you may select any element in nums and increment it by 11. You can perform, at most, k such operations.

Your task is to maximize the product of all elements in the array after performing up to k operations. As the resulting product can be very large, return the product modulo 109+710^9+7.

Note: Ensure that the product is maximized before applying the modulo operation.

Constraints:

  • 11 \leq nums.length, k 103\leq 10^3

  • 00 \leq nums[i] 103\leq10^3

Solution

The core intuition behind the solution is to maximize the product of the nums elements by distributing the k increment operations in a way that promotes balance among the values. In multiplication, products are higher when numbers are close in value. For example, 4×4×4=644×4×4=64 (balanced) is greater than 2×5×5=502×5×5=50 (unbalanced), and significantly greater than 1×6×5=301×6×5=30, even though the sum of the numbers is the same in each case. This shows that multiplication favors balanced numbers. So, to achieve this, we follow a strategy based on two key observations:

  • Multiplication favors numbers that are close in value, so the goal is to balance out nums by incrementing the smallest elements first.

  • Incrementing smaller numbers contributes more to the overall product than incrementing larger ones, making them the most effective targets for k operations.

This strategy uses the top k elements pattern with a min heap, allowing quick access to the current smallest number. All elements from nums are inserted into the heap. For each k operations, we extract the minimum element, increment it by 11, and push it back. This process ensures that the increments are done where they produce the greatest gain.

In some cases, the smallest element may be much smaller than the rest of the array—for example, in [1,20,30,40][1, 20, 30, 40]. In such cases, all k operations may be applied solely to that minimum element, progressively closing the gap and improving the product.

After performing all k increments, the product of all heap elements is computed and returned modulo 109+710^9 + 7.

Now, let’s look at the solution steps below:

  1. We initialize a variable mod = 109 + 7 to handle modulo operations for the overflow issues.

  2. We initialize a min heap with all the elements of nums.

  3. To evenly distribute increments, we perform k operations by:

    1. Popping the smallest element from the heap.

    2. Incrementing the smallest element by 11.

    3. Pushing the smallest element back into the heap.

  4. We initialize a variable, result = 1, to store the maximized product of all elements in nums.

  5. After k increments, we compute the product modulo of all elements in the heap by:

    1. Repeatedly popping elements from the heap.

    2. Multiply them with the result, taking the modulo with mod at each step to prevent overflow.

  6. We return the final value of the result, which represents the maximum product modulo.

Let’s look at the following illustration to get a better understanding of the solution:

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.