Maximum Product Subarray Problem Statement

Given an array of integers, determine the contiguous subarray that produces the maximum product of its elements.

Explanation:

A subarray can be derived from the original array by deleting certain elements from the beginning and/or the end. You need to identify which subarray gives the highest product.

Input:

The first line contains an integer 'T', denoting the number of test cases.
For each test case, the first line contains an integer N, representing the number of elements in the array 'arr'.
The second line contains N space-separated integers, which are the elements of 'arr'.

Output:

Output the maximum product of a contiguous subarray for each test case. The output for each test case should be printed on a new line.

Example:

Input:
If arr = {-3, 4, 5}
Subarray Products:
The possible non-empty contiguous subarrays: {-3}, {4}, {5}, {-3, 4}, {4, 5}, {-3, 4, 5}
Their products: -3, 4, 5, -12, 20, -60
Output:
Maximum product is 20
Constraints:
  • 1 <= T <= 100
  • 1 <= N <= 5000
  • -100 <= arr[i] <= 100
Note:

You do not need to print anything explicitly. The function implementation should return the result.

Follow-Up:

Consider solving the problem in linear time with constant space complexity.

Be the first one to answer
Add answer anonymously...
Directi Software Developer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter