Skip to main content
All Questions

Find the maximum product subarray in a given array.

Medium
Unlock detailed company stats for this questionUpgrade

Given an integer array nums, find the contiguous subarray with the largest product and return that product. A subarray contains at least one element.

Example 1

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

The subarray [2, 3] has the largest product.

Example 2

Input: nums = [-2, 0, -1] Output: 0

No subarray has a positive product. The best you can do is 0, from any subarray that includes the 0.

Constraints

  • 1 <= nums.length <= 2 * 10^4
  • -10 <= nums[i] <= 10
  • The product of any subarray fits in a 32-bit integer.

Related courses

Course

Software Engineering Interview Prep

Land your dream software engineering role at Google, Amazon, Microsoft, Meta, Apple, and other top companies. Learn from mock interviews, frameworks, and advice from senior candidates—practice data structures, algorithms, system design, people management, behavioral interviews, and more.

Course

System Design Interviews

Learn how to answer the latest system design questions across product, infrastructure, and AI domains. Features in-depth video examples, written breakdowns, and helpful reference patterns. Watch senior engineers and managers answer these questions in mock interview videos.