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 <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;
}
|
Java
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));
}
}
|
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#
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;
}
public static void Main()
{
int [] nums = {6, 3, 3, 2};
int k = 2;
Console.Write( "the minimum product is " + minimumProduct(nums, k));
}
}
|
Javascript
<script>
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;
}
let nums = [6, 3, 3, 2];
let k = 2;
document.write( "the minimum product is " + minimumProduct(nums, k));
</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;
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;
}
|
Java
import java.util.*;
class GFG
{
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;
}
public static void main(String[] args)
{
int [] arr = { 6 , 3 , 3 , 2 };
int N = arr.length;
int K = 2 ;
System.out.println(minimizeProduct(arr, N, K));
}
}
|
Python3
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
if __name__ = = '__main__' :
arr = [ 6 , 3 , 3 , 2 ]
N = len (arr)
K = 2
print (minimizeProduct(arr, N, K))
|
C#
using System;
public class GFG
{
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;
}
public static void Main(String[] args)
{
int [] arr = {6, 3, 3, 2};
int N = arr.Length;
int K = 2;
Console.WriteLine(minimizeProduct(arr, N, K));
}
}
|
Javascript
<script>
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;
}
var arr = [6, 3, 3, 2];
var N = arr.length;
var K = 2;
document.write(minimizeProduct(arr, N, K));
</script>
|
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());
int max_idx = distance(arr.begin(), max_element(arr.begin(), arr.end()));
arr[max_idx] += K;
int product = 1;
for ( int i = 0; i < n; i++) {
product *= arr[i];
}
return product;
}
int main() {
vector< int > arr = {6, 3, 3, 2};
int K = 2;
cout << minimizeProduct(arr, K) << endl;
return 0;
}
|
Java
import java.util.ArrayList;
import java.util.Collections;
public class MinimizeProduct {
public static int minimizeProduct(ArrayList<Integer> arr, int K) {
int n = arr.size();
int max_val = Collections.max(arr);
int max_idx = arr.indexOf(max_val);
arr.set(max_idx, arr.get(max_idx) + K);
int product = 1 ;
for ( int i = 0 ; i < n; i++) {
product *= arr.get(i);
}
return product;
}
public static void main(String[] args) {
ArrayList<Integer> arr = new ArrayList<>();
arr.add( 6 );
arr.add( 3 );
arr.add( 3 );
arr.add( 2 );
int K = 2 ;
System.out.println(minimizeProduct(arr, K));
}
}
|
Python
def minimize_product(arr, K):
n = len (arr)
max_val = max (arr)
max_idx = arr.index(max_val)
arr[max_idx] + = K
product = 1
for i in range (n):
product * = arr[i]
return product
if __name__ = = "__main__" :
arr = [ 6 , 3 , 3 , 2 ]
K = 2
print (minimize_product(arr, K))
|
C#
using System;
using System.Collections.Generic;
using System.Linq;
class Program {
static int MinimizeProduct(List< int > arr, int K)
{
int n = arr.Count;
int maxVal = arr.Max();
int maxIdx = arr.IndexOf(maxVal);
arr[maxIdx]
+= K;
int product = 1;
foreach ( int element in arr)
{
product *= element;
}
return product;
}
static void Main()
{
List< int > arr = new List< int >{ 6, 3, 3, 2 };
int K = 2;
Console.WriteLine(MinimizeProduct(arr, K));
}
}
|
Javascript
function minimizeProduct(arr, K) {
const n = arr.length;
const maxVal = Math.max(...arr);
const maxIdx = arr.indexOf(maxVal);
arr[maxIdx] += K;
let product = 1;
for (let i = 0; i < n; i++) {
product *= arr[i];
}
return product;
}
const arr = [6, 3, 3, 2];
const K = 2;
console.log(minimizeProduct(arr, K));
|
Time complexity: O(n)
Auxiliary Space: O(1)
Last Updated :
29 Oct, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...