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 array arr.
  • 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.
AnswerBot
4mo

The task is to find the contiguous subarray within an array with the highest product of its elements.

  • Iterate through the array and keep track of the maximum and minimum product ending at each index.

  • Up...read more

Help your peers!
Select
Add answer anonymously...

Cognizant GenC Next interview questions & answers

A GenC Next was asked Q. Write a program to reverse your name.
A GenC Next was asked Q. What is your favorite technology?
A GenC Next was asked Q. What is virtual DOM and why does ReactJS use it?

Popular interview questions of GenC Next

A GenC Next was asked Q1. Write a program to reverse your name.
A GenC Next was asked Q2. What is your favorite technology?
A GenC Next was asked Q3. What is virtual DOM and why does ReactJS use it?
Cognizant GenC Next Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

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

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits