Maximum product of an Array after subtracting 1 from any element N times
Last Updated :
06 Dec, 2021
Given an array arr[] of positive integers of size M and an integer N, the task is to maximize the product of array after subtracting 1 from any element N number of times
Examples:
Input: M = 5, arr[] = {5, 1, 7, 8, 3}, N = 2
Output: 630
Explanation: After subtracting 1 from arr[3] 2 times, array becomes {5, 1, 7, 6, 3} with product = 630
Input: M = 2, arr[] = {2, 2}, N = 4
Output: 0
Explanation: After subtracting 2 from arr[0] and arr[1] 2 times each, array becomes {0, 0} with product = 0
Approach: The task can be solved with the help of max-heap Follow the below steps to solve the problem:
- Insert all the elements inside a max-heap
- Pop the top element from the max- heap, and decrement 1 from it, also decrement N
- Insert the popped element back to max-heap
- Continue this process till N > 0
- The maximum product will be the product of all the elements inside the max-heap
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int getMax( int m, int arr[], int n)
{
priority_queue< int > q;
for ( int i = 0; i < m; i++)
q.push(arr[i]);
while (n--) {
int x = q.top();
q.pop();
--x;
q.push(x);
}
int ans = 1;
while (!q.empty()) {
ans *= q.top();
q.pop();
}
return ans;
}
int main()
{
int M = 5;
int arr[5] = { 5, 1, 7, 8, 3 };
int N = 2;
cout << getMax(M, arr, N);
}
|
Java
import java.util.*;
class GFG
{
static Integer getMax( int m, Integer arr[], int n)
{
PriorityQueue<Integer> q
= new PriorityQueue<Integer>(
Collections.reverseOrder());
for ( int i = 0 ; i < m; i++)
q.add(arr[i]);
while (n-- != 0 ) {
Integer x = q.poll();
--x;
q.add(x);
}
Integer ans = 1 ;
while (q.size() != 0 ) {
ans *= q.poll();
}
return ans;
}
public static void main(String[] args)
{
int M = 5 ;
Integer arr[] = { 5 , 1 , 7 , 8 , 3 };
int N = 2 ;
System.out.println(getMax(M, arr, N));
}
}
|
Python3
from queue import PriorityQueue
def getMax(m, arr, n):
q = PriorityQueue()
for i in range ( 0 , m):
q.put( - arr[i])
while (n):
n - = 1
x = - q.get()
x - = 1
q.put( - x)
ans = 1
while ( not q.empty()):
ans * = - q.get()
return ans
if __name__ = = "__main__" :
M = 5
arr = [ 5 , 1 , 7 , 8 , 3 ]
N = 2
print (getMax(M, arr, N))
|
C#
using System;
using System.Collections.Generic;
public class GFG
{
static int getMax( int m, int []arr, int n)
{
List< int > q
= new List< int >();
for ( int i = 0; i < m; i++){
q.Add(arr[i]);
}
q.Sort();
q.Reverse();
while (n-- != 0) {
int x = q[0];
q.RemoveAt(0);
--x;
q.Add(x);
q.Sort();
q.Reverse();
}
int ans = 1;
while (q.Count != 0) {
ans *= q[0];
q.RemoveAt(0);
}
return ans;
}
public static void Main(String[] args)
{
int M = 5;
int []arr = { 5, 1, 7, 8, 3 };
int N = 2;
Console.WriteLine(getMax(M, arr, N));
}
}
|
Javascript
<script>
function getMax(m, arr, n)
{
let q = [];
for (let i = 0; i < m; i++) {
q.push(arr[i]);
}
q.sort();
q.reverse();
while (n-- != 0) {
let x = q[0];
q.splice(0, 1);
--x;
q.push(x);
q.sort();
q.reverse();
}
let ans = 1;
while (q.length != 0) {
ans *= q[0];
q.splice(0, 1);
}
return ans;
}
let M = 5;
let arr = [5, 1, 7, 8, 3];
let N = 2;
document.write(getMax(M, arr, N));
</script>
|
Time Complexity: O(nlogm)
Auxiliary Space: O(m)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...