Maximum Product Subarray Problem Statement
Given an array arr
of integers, your task is to identify the contiguous subarray within the array which has the highest product of its elements. Return this maximum product value.
Input:
The first line contains an integer 'T' which indicates the number of test cases to be executed.
For each test case:
- The first line contains a single integer N, denoting the number of elements in the array arr
.
- The second line contains N space-separated integers representing elements of the array arr
.
Output:
For each test case, output a single integer which is the maximum product of any contiguous non-empty subarray within the array arr
. Output each result on a new line.
Example:
If arr = [-3, 4, 5]
:
All possible non-empty contiguous subarrays of arr
are [-3], [4], [5], [-3, 4], [4, 5], and [-3, 4, 5].
The product of these subarrays are -3, 4, 5, -12, 20, and -60, respectively.
The maximum product is 20. Hence, the answer is 20.
Constraints:
1 ≤ T ≤ 100
1 ≤ N ≤ 5000
-100 ≤ arr[i] ≤ 100
- The size
N
represents the number of elements in the arrayarr
. - Time limit is 1 second per test case.
Note:
You are not required to print anything in your solution. The input/output handling is already managed. Implement the function as required.
Be the first one to answer
Add answer anonymously...
Top Cognizant GenC Next interview questions & answers
Top HR questions asked in Cognizant GenC Next
Stay ahead in your career. Get AmbitionBox app
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
Get AmbitionBox app