Maximum Product

You are given an integer array nums of size n.

Your task is to choose exactly 5 elements from the array such that their indices are in increasing order: i < j < k < l < t

Find the maximum possible value of: nums[i] * nums[j] * nums[k] * nums[l] * nums[t]

Return that maximum product.

Example 1:

Input: nums = [1, 2, 3, 4, 5, 6]

Output: 720

Explanation:

One optimal choice is indices (1, 2, 3, 4, 5) using values 2, 3, 4, 5, 6.

Their product is: 2 * 3 * 4 * 5 * 6 = 720

No other choice of 5 elements gives a larger product.

Example 2:

Input: nums = [-10, -10, 1, 2, 3, 4]

Output: 2400

Explanation:

One optimal choice is indices (0, 1, 3, 4, 5) using values -10, -10, 2, 3, 4.

Their product is: (-10) * (-10) * 2 * 3 * 4 = 2400

Although the array contains positive values, using two negative values can produce a larger positive result.

Now Your Turn!

Pick the correct output for the given input

Input: nums = [-5, -4, -3, 2, 6, 7]

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 5 <= n <= 105
  • -3 * 103 <= nums[i] <= 3 * 103
  • The sum of n over all test cases does not exceed 2 * 105

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

0
class Solution {
public:
long long maxProduct(vector<int>& nums) {
// Your code goes here
}
};
 
Test Case

Input:

Nums