# Minimize the product of Array elements after K increments

• Last Updated : 08 Sep, 2022

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 : arr)``        ``{``          ``if``(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 : arr)``    ``{``      ``if``(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` `arr:``        ``if` `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;``    ``foreach``(``int` `i ``in` `arr)``    ``{``      ``if``(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)

My Personal Notes arrow_drop_up