DevHireLab
TutorialsBootcamp
Problems
Code SimulatorAI InterviewSoonContact
DevHireLab
TutorialsBootcamp
Problems
Code SimulatorAI InterviewSoonContact
Back to Arena
medium
Array

Maximum Product Subarray

**Problem Statement:** Given an integer array `nums`, find a subarray that has the largest product, and return the product. **Hint / Expected Approach:** Track both max and min (negative × negative) **Edge Cases to Consider:** - (1) All negatives - (2) Contains zero - (3) Single element

Examples

Example 1
Input: [2,3,-2,4]
Output: 6
Example 2
Input: [-2,0,-1]
Output: 0
Example 3
Input: [0,2]
Output: 2

Constraints

  • ▪1 <= nums.length <= 10^4
  • ▪-10^9 <= nums[i] <= 10^9
  • ▪Time complexity should be O(n) or O(n log n)

Watch Out For Edge Cases

  • ▪All negatives
  • ▪Contains zero
  • ▪Single element
Frequently Asked At
AmazonGoogleMicrosoftMetaFlipkart