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 0Input: 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> |
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=1;i<N;i++) { if (arr[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= 1 ;i<N;i++) { if (arr[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<N;i++) { if (arr[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 = 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> |
144
Time Complexity: O(N)
Auxiliary Space: O(1)
Another Approach:
- Start the main function.
- Create a vector of integers arr and initialize it with {6, 3, 3, 2}.
- Create an integer variable K and initialize it with 2.
- Call the minimizeProduct function with arr and K as arguments.
- Start the minimizeProduct function.
- Get the size of the vector arr and store it in n.
- Find the maximum element in arr using the max_element function from the <algorithm> library, and store it in max_val.
- Find the index of the maximum element in arr using the distance function and store it in max_idx.
- Increment the maximum element in arr K times by adding K to arr[max_idx].
- Initialize an integer variable product to 1.
- Multiply all the elements in arr and store the result in product using a range-based for loop.
- Return the value of product.
- End the minimizeProduct function.
- Print the result returned by the minimizeProduct function.
- End the main function.
Below is the implementation of the above code:
C++
#include <iostream> #include <vector> #include <algorithm> 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; } |
144
Time complexity: O(n)
Auxiliary Space: O(1)
Please Login to comment...