Diwali Offer!Flat 20% OFF
Enroll Now
PrakalpanaLive online tech training

176. Maximum Product Subarray

MediumDay 52 of 60Dynamic Programming

Given an integer array nums, find a contiguous non-empty subarray whose product is as large as possible and return that product. The answer is guaranteed to fit in a 32-bit integer.

Example 1
Input: nums = [2,3,-2,4]
Output: 6
The subarray [2,3] has product 6.
Example 2
Input: nums = [-2,0,-1]
Output: 0
[-2,-1] is not contiguous, so the best is the single element 0.
Constraints
  • 1 <= nums.length <= 2 * 10^4
  • -10 <= nums[i] <= 10
  • Every prefix or suffix product fits in a 32-bit integer
Hints
Frequently asked at: Amazon, LinkedIn, Microsoft, Google
Ctrl + Enter runs the examples

Not sure what to learn next?

A 1-on-1 session with an industry mentor and a roadmap built for you. How it works

  1. 1Discuss your goals
  2. 2Get a personal plan
  3. 3Start learning
Book for β‚Ή200/ 30 min