Maximum Product Subarray

Input: nums = [2,3,-2,4]
Output: 6
Explanation: [2,3] has the largest product 6.
Input: nums = [-2,0,-1]
Output: 0
Explanation: The result cannot be 2, because [-2,-1] is not a subarray.

def maxProduct(self, nums: List[int]) -> int:
max_=-1*float('inf')
for i in range(len(nums)):
prod=1
for j in range(i,len(nums)):
prod*=nums[j]
if(max_<prod):
max_=prod
return(max_)
def maxProduct(self, nums):
global_,max_,min_=nums[0],nums[0],nums[0]
i=1
while(i<len(nums)):
prev_max=max_
max_=max(nums[i],max_*nums[i],min_*nums[i])
min_=min(nums[i],prev_max*nums[i],min_*nums[i])
global_=max(global_,max_)
i+=1
return global_
n=len(nums)
left,right=1,1
global_max=-1*float('inf')
for i in range(n):
if(left==0):
left=1
if(right==0):
right=1
left*=nums[i]
right=nums[n-1-i]
global_max=max(global_max,left,right)
return global_max

--

--

--

Tech-Enthusiast, Coder,Explorer,Geeky,Software Engineer |A piece of code delivers everything that you need. The world is all about codes.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Generic Traits and Classes With Type Parameters And Variance

Java Design Patterns

Authentication in Ruby on Rails

Saving Data in Unity3D Using PlayerPrefs

MERN Stack Project

wXCASH Update #3

How to build a beautiful Twitter scraping web app using Streamlit and Twint.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Dhanarajappu

Dhanarajappu

Tech-Enthusiast, Coder,Explorer,Geeky,Software Engineer |A piece of code delivers everything that you need. The world is all about codes.

More from Medium

8 Things to Do Before An Interview

Behavior Questions Prep 28/1

How to Approach Difficult Tasks at Work

My Interview experience at Accolite, Inc.