 GFG App
Open App Browser
Continue

# Minimize the product of Array elements after K increments

Given an array of non-negative integers arr[] and an integer K, the task is to minimize the product of array elements by performing at K increment operations on the array elements.

Examples:

Input: arr[] = [0, 9], K = 5
Output: 0
Explanation: Since 0 is present the, minimum product
that can be obtained after performing at most K increments is 0

Input: arr[] = [6, 3, 3, 2], K = 2
Output: 144
Explanation: Choosing 6 and performing at most K increments on it.
The new array after performing K increments is [8, 3, 3, 2]

Naïve Approach: The problem can be solved using the greedy approach based on the below idea:

To minimize the product, we should not modify the less valued minimum elements. Just perform all the K increments in the element which is maximum after each increment.

Follow the below steps to implement the idea:

• Run a for loop up to K times.
• Each time select the maximum and perform the Kth increment.
• Perform the multiplication operation and return the product

Below is the implementation for the above approach:

## C++

 `#include ` `using` `namespace` `std;` `int` `minimumProduct(vector<``int``> nums,``int` `k){`   `  ``for``(``int` `i = 0; i < k; i++){ ` `    ``int` `m=*max_element(nums.begin(), nums.end());` `    ``auto` `it = find(nums.begin(), nums.end(), m);` `    ``int` `index = it - nums.begin();` `    ``nums[index] += 1;` `  ``}` `  ``int` `a = 1;` `  ``for``(``int` `i = 0; i < nums.size(); i++)` `    ``a *= nums[i];` `  ``return` `a;` `}`     `int` `main() {` `  ``vector<``int``> nums = {6, 3, 3, 2};` `  ``int` `k = 2;` `  ``cout << ``"the minimum product is "` `;` `  ``cout <

## Java

 `/*package whatever //do not write package name here */` `import` `java.io.*;` `import` `java.util.Arrays;`   `class` `GFG {`   `  ``static` `int` `minimumProduct(``int``[] nums,``int` `k){`   `    ``for``(``int` `i = ``0``; i < k; i++){ ` `      ``int` `m = Arrays.stream(nums).max().getAsInt();` `      ``int` `index  = -``1``;` `      ``for``(``int` `j = ``0``; j < nums.length; j++)` `        ``if``(nums[j] == m)` `          ``index = j;`   `      ``nums[index] += ``1``;` `    ``}` `    ``int` `a = ``1``;` `    ``for``(``int` `i = ``0``; i < nums.length; i++)` `      ``a *= nums[i];` `    ``return` `a;` `  ``}`   `  ``public` `static` `void` `main (String[] args) {` `    ``int``[] nums = {``6``, ``3``, ``3``, ``2``};` `    ``int` `k = ``2``;` `    ``System.out.println(``"the minimum product is "` `+ minimumProduct(nums, k));   ` `  ``}` `}`   `// This code is contributed by hrithikgarg03188.`

## Python3

 `def` `minimumProduct(nums, k):` `    ``for` `_ ``in` `range``(k):` `        ``nums[nums.index(``max``(nums))] ``+``=` `1` `    ``a ``=` `1` `    ``for` `i ``in` `nums:` `        ``a ``*``=` `i` `    ``return` `a`     `nums ``=` `[``6``, ``3``, ``3``, ``2``]` `k ``=` `2`   `print``(``"the minimum product is"``, minimumProduct(nums, k))`

## C#

 `// C# program for above approach` `using` `System;` `using` `System.Linq;`   `class` `GFG` `{`   ` ``static` `int` `minimumProduct(``int``[] nums,``int` `k){`   `    ``for``(``int` `i = 0; i < k; i++){ ` `      ``int` `m = nums.Max();` `      ``int` `index  = -1;` `      ``for``(``int` `j = 0; j < nums.Length; j++)` `        ``if``(nums[j] == m)` `          ``index = j;`   `      ``nums[index] += 1;` `    ``}` `    ``int` `a = 1;` `    ``for``(``int` `i = 0; i < nums.Length; i++)` `      ``a *= nums[i];` `    ``return` `a;` `  ``}`   `// Driver Code` `public` `static` `void` `Main()` `{` `    ``int``[] nums = {6, 3, 3, 2};` `    ``int` `k = 2;` `    ``Console.Write(``"the minimum product is "` `+ minimumProduct(nums, k)); ` `}` `}`   `// This code is contributed by code_hunt.`

## Javascript

 ``

Output

`the minimum product is 144`

Time Complexity: O(N*K)
Auxiliary Space: O(1)

Efficient Approach: The idea behind the efficient is similar as the one mentioned above. But the time complexity can be reduced based on the following fact:

If the maximum element is incremented in any operation, no other element can become maximum on any of the further operations. So just increment the maximum value K times.

Follow the below steps to implement the idea:

• Find the maximum value from the array.
• Increment the maximum value K times.
• Multiply the elements of the array and get the answer.

Below is the implementation of the above approach.

## C++

 `#include ` `using` `namespace` `std;`   `// Function to find the minimum product` `int` `minimizeProduct(vector<``int``> arr, ``int` `N, ``int` `K){`   `         ``int` `maxi = 0;` `        ``for``(``int` `i=1;i arr[maxi])` `            ``maxi = i;` `        ``}`   `        ``arr[maxi] += K;` `        ``int` `ans = 1;`   `        ``for``(``int` `i : arr)` `        ``{` `          ``ans *= i;` `        ``}`   `        ``return` `ans;` `}`   `int` `main() {` `  ``vector<``int``> arr =  {6, 3, 3, 2};` `  ``int` `N = arr.size();` `  ``int` `k = 2;` `  ``cout <

## Java

 `// JAVA code to implement the approach` `import` `java.util.*;` `class` `GFG` `{`   `  ``// Function to find the minimum product` `  ``static` `int` `minimizeProduct(``int` `[] arr, ``int` `N, ``int` `K)` `  ``{` `    ``int` `maxi = ``0``;` `    ``for``(``int` `i=``1``;i arr[maxi])` `        ``maxi = i;` `    ``}`   `    ``arr[maxi] += K;` `    ``int` `ans = ``1``;`   `    ``for``(``int` `i : arr)` `    ``{` `      ``ans *= i;` `    ``}`   `    ``return` `ans;` `  ``}`   `  ``// Driver Code` `  ``public` `static` `void` `main(String[] args)` `  ``{` `    ``int``[] arr = {``6``, ``3``, ``3``, ``2``};` `    ``int` `N = arr.length;` `    ``int` `K = ``2``;`   `    ``// Function call` `    ``System.out.println(minimizeProduct(arr, N, K));` `  ``}` `}`   `// This code is contributed by sanjoy_62.`

## Python3

 `# Python code to implement the approach`   `# Function to find the minimum product` `def` `minimizeProduct(arr, N, K):` `    ``maxi ``=` `0` `    ``for` `i ``in` `range``(N):` `        ``if` `arr[i] > arr[maxi]:` `            ``maxi ``=` `i`   `    ``arr[maxi] ``+``=` `K` `    ``ans ``=` `1` `    ``for` `i ``in` `arr:` `        ``ans ``*``=` `i`   `    ``return` `ans`     `# Driver code` `if` `__name__ ``=``=` `'__main__'``:` `    ``arr ``=` `[``6``, ``3``, ``3``, ``2``]` `    ``N ``=` `len``(arr)` `    ``K ``=` `2`   `    ``# Function call` `    ``print``(minimizeProduct(arr, N, K))`

## C#

 `// C# code to implement the approach` `using` `System;`   `public` `class` `GFG` `{`   `  ``// Function to find the minimum product` `  ``static` `int` `minimizeProduct(``int` `[] arr, ``int` `N, ``int` `K)` `  ``{` `    ``int` `maxi = 0;` `    ``for``(``int` `i=1;i arr[maxi])` `        ``maxi = i;` `    ``}`   `    ``arr[maxi] += K;` `    ``int` `ans = 1;`   `    ``foreach``(``int` `i ``in` `arr)` `    ``{` `      ``ans *= i;` `    ``}`   `    ``return` `ans;` `  ``}`   `  ``// Driver Code` `  ``public` `static` `void` `Main(String[] args)` `  ``{` `    ``int``[] arr = {6, 3, 3, 2};` `    ``int` `N = arr.Length;` `    ``int` `K = 2;`   `    ``// Function call` `    ``Console.WriteLine(minimizeProduct(arr, N, K));` `  ``}` `}`   `// This code is contributed by shikhasingrajput`

## Javascript

 ``

Output

`144`

Time Complexity: O(N)
Auxiliary Space: O(1)

Another Approach:

1. Start the main function.
2. Create a vector of integers arr and initialize it with {6, 3, 3, 2}.
3. Create an integer variable K and initialize it with 2.
4. Call the minimizeProduct function with arr and K as arguments.
5. Start the minimizeProduct function.
6. Get the size of the vector arr and store it in n.
7. Find the maximum element in arr using the max_element function from the <algorithm> library, and store it in max_val.
8. Find the index of the maximum element in arr using the distance function and store it in max_idx.
9. Increment the maximum element in arr K times by adding K to arr[max_idx].
10. Initialize an integer variable product to 1.
11. Multiply all the elements in arr and store the result in product using a range-based for loop.
12. Return the value of product.
13. End the minimizeProduct function.
14. Print the result returned by the minimizeProduct function.
15. End the main function.

Below is the implementation of the above code:

## C++

 `#include ` `#include ` `#include `   `using` `namespace` `std;`   `int` `minimizeProduct(vector<``int``>& arr, ``int` `K) {` `    ``int` `n = arr.size();` `    ``int` `max_val = *max_element(arr.begin(), arr.end()); ``// find maximum element` `    ``int` `max_idx = distance(arr.begin(), max_element(arr.begin(), arr.end())); ``// find its index` `    `  `    ``arr[max_idx] += K; ``// increment the maximum value K times` `    `  `    ``int` `product = 1;` `    ``for` `(``int` `i = 0; i < n; i++) {` `        ``product *= arr[i]; ``// multiply all elements` `    ``}` `    `  `    ``return` `product;` `}`   `int` `main() {` `    ``vector<``int``> arr = {6, 3, 3, 2};` `    ``int` `K = 2;` `    `  `    ``cout << minimizeProduct(arr, K) << endl;` `    `  `    ``return` `0;` `}`

Output

`144`

Time complexity: O(n)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up