
Introduction
In this problem, we are given an array (containing all positive integers) and an integer N. Our task is to obtain the maximum product of an array after subtracting one from any element N times.
We will be discussing two approaches to solving the problem - Naive and Efficient ones. So let's get started by first understanding the problem statement and a few test cases.
Problem Statement
Given an array of integers and an integer N, Find the maximum product of the array after subtracting 1 from any number of the array N times.
Test Case
Let us take a few sample test cases to understand the question.
Test Case 1
Input: M = 4, arr[] = {1, 7, 2, 6}, N = 2
Output: 60
Explanation: 1 is subtracted from element 7 two times. The final array becomes {1,5,2,6}. So the final product is 1*5*2*6 = 60.
Test Case 2
Input: M = 4, arr[] = {1, 7, 2, 6}, N = 4
Output: 40
Explanation: 1 is subtracted from 7 two times, resulting in the array {1,5,2,6}. Then 1 is subtracted from element 6 once. Then the array becomes {1,5,2,5}. Now 1 is subtracted from element 5 (there are two 5s in the array, consider anyone). The final array becomes {1,4,2,5}. So the final product is 1*4*2*5 = 40.
Test Case 3
Input: M = 3, arr[] = {1,1,1} N = 1
Output: 0
Explanation: 1 is deleted from the first element. The array becomes {0,1,1,}. So the final product is 0*1*1 = 0.
Let us see how to solve the question:
Also read, Euclid GCD Algorithm
Naive Approach
To obtain the maximum product, we need to subtract one from the current largest element from the given array N times. Subtracting one from any other element other than the current largest element will not give us the maximum product.
We will now discuss the brute force approach that can be used to implement the idea. For the brute force approach, we need to sort the array in increasing order and subtract one from the last element of the array N times.
After these operations have been done, the final product is calculated using the modified array.
Steps of algorithm
Perform the below steps N times
- Sort array in increasing order
- Subtract one from the largest element
- Calculate the final product
-
Run a loop from 1 to M
- ans = ans * arr[i]
- Display ans
Implementation in C++
#include <bits/stdc++.h>
using namespace std;
int solve(int M, int N, int arr[])
{
int ans = 1;
//performing below steps for N times
for (int i = 0; i < N; i++)
{
//sort array in increasing order
sort(arr, arr + M);
//subtracting 1 from the largest element
arr[M - 1] = arr[M - 1] - 1;
}
//calculating the final product
for (int i = 0; i < M; i++)
ans = ans * arr[i];
cout << ans << endl;
}
int main()
{
int M = 4, N = 2;
int arr[5] = {1, 7, 2, 6};
solve(M, N, arr);
}
Output:

Complexity Analysis
Time Complexity: O(N*(MlogM))
MlogM time is needed to sort the array of size M, and this sorting is performed N times.
Here, N refers to the given integer, and M refers to the size of the array.
Space Complexity: O(1)
No extra space is required in this approach.






