Skip to content
Related Articles

Related Articles

Minimize the product of Array elements after K increments

View Discussion
Improve Article
Save Article
  • Last Updated : 08 Sep, 2022
View Discussion
Improve Article
Save Article

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 <bits/stdc++.h>
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 <<minimumProduct(nums, k);
  return 0;
}
 
// This code is contributed by satwik4409.

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




<script>
// Javascript program for above approach
function minimumProduct(nums, k) {
 
    for (let i = 0; i < k; i++) {
        let m = Math.max(...nums);
        console.log(m)
        let index = -1;
        for (let j = 0; j < nums.length; j++)
            if (nums[j] == m)
                index = j;
 
        nums[index] += 1;
    }
    let a = 1;
    for (let i = 0; i < nums.length; i++)
        a *= nums[i];
    return a;
}
 
// Driver Code
let nums = [6, 3, 3, 2];
let k = 2;
document.write("the minimum product is " + minimumProduct(nums, k));
 
// This code is contributed by Saurabh jasiwal
</script>

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 <bits/stdc++.h>
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 <<minimizeProduct(arr, N, k);
  return 0;
}
 
// This code is contributed by shikhasingrajput

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




<script>
// javascript code to implement the approach
 // Function to find the minimum product
  function minimizeProduct(arr , N , K)
  {
    var maxi = 0;
    for(var i = 0; i < N; i++)
    {
      if(arr[i] > arr[maxi])
        maxi = arr[i];
    }
 
    arr[maxi] = arr[maxi] + K;
    var ans = 1;
 
    for(var i = 0; i < N; i++)
    {
      ans = ans*arr[i];
    }
 
    return ans;
  }
 
  // Driver Code
    var arr = [6, 3, 3, 2];
    var N = arr.length;
    var K = 2;
 
    // Function call
    document.write(minimizeProduct(arr, N, K));
 
// This code is contributed by shikhasingrajput
</script>

Output

144

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


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!