Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python maximum product subarray

# Python3 program to find Maximum Product Subarray
 
# Returns the product of max product subarray.
def maxSubarrayProduct(arr, n):
 
    # Initializing result
    result = arr[0]
 
    for i in range(n):
     
        mul = arr[i]
       
        # traversing in current subarray
        for j in range(i + 1, n):
         
            # updating result every time
            # to keep an eye over the maximum product
            result = max(result, mul)
            mul *= arr[j]
         
        # updating the result for (n-1)th index.
        result = max(result, mul)
     
    return result
 
# Driver code
arr = [ 1, -2, -3, 0, 7, -8, -2 ]
n = len(arr)
print("Maximum Sub array product is" , maxSubarrayProduct(arr, n))
 
# This code is contributed by divyeshrabadiya07
Comment

maximum product subarray

<script>
  
// JavaScript program to find 
// Maximum Product Subarray
  
/* Returns the product 
  of max product subarray.
Assumes that the given 
array always has a subarray
with product more than 1 */
function maxSubarrayProduct(arr, n)
{
    // max positive product 
    // ending at the current position
    let max_ending_here = 1;
  
    // min negative product ending 
    // at the current position
    let min_ending_here = 1;
  
    // Initialize overall max product
    let max_so_far = 0;
    let flag = 0;
    /* Traverse through the array. 
    Following values are
    maintained after the i'th iteration:
    max_ending_here is always 1 or 
    some positive product ending with arr[i]
    min_ending_here is always 1 or 
    some negative product ending with arr[i] */
    for (let i = 0; i < n; i++)
    {
        /* If this element is positive, update
        max_ending_here. Update min_ending_here only if
        min_ending_here is negative */
        if (arr[i] > 0) 
        {
            max_ending_here = max_ending_here * arr[i];
            min_ending_here
                = Math.min(min_ending_here * arr[i], 1);
            flag = 1;
        }
  
        /* If this element is 0, then the maximum product
        cannot end here, make both max_ending_here and
        min_ending_here 0
        Assumption: Output is alway greater than or equal
                    to 1. */
        else if (arr[i] == 0) {
            max_ending_here = 1;
            min_ending_here = 1;
        }
  
        /* If element is negative. This is tricky
         max_ending_here can either be 1 or positive.
         min_ending_here can either be 1 or negative.
         next max_ending_here will always be prev.
         min_ending_here * arr[i] ,next min_ending_here
         will be 1 if prev max_ending_here is 1, otherwise
         next min_ending_here will be prev max_ending_here *
         arr[i] */
  
        else {
            let temp = max_ending_here;
            max_ending_here
                = Math.max(min_ending_here * arr[i], 1);
            min_ending_here = temp * arr[i];
        }
  
        // update max_so_far, if needed
        if (max_so_far < max_ending_here)
            max_so_far = max_ending_here;
    }
    if (flag == 0 && max_so_far == 0)
        return 0;
    return max_so_far;
}
  
  
    // Driver program 
      
    let arr = [ 1, -2, -3, 0, 7, -8, -2 ];
    let n = arr.length;
    document.write("Maximum Sub array product is " + 
                    maxSubarrayProduct(arr,n)); 
      
</script>
Comment

PREVIOUS NEXT
Code Example
Python :: python count unique values in list 
Python :: print value of array python 
Python :: how to check if element is in list python 
Python :: python histogram one liners 
Python :: fonction nombre premier python 
Python :: pandas trim string of all cells 
Python :: python append to tuple list 
Python :: get all commands discord.py 
Python :: python filter list with list of booleans 
Python :: Working with WTForms FieldList 
Python :: how to run mac terminal from python script 
Python :: python [] for loop 
Python :: dash authentication 
Python :: add element to array list python 
Python :: Selenium get response body python 
Python :: datetime64 ns to date python 
Python :: sftp python 
Python :: how to display items on a list on new lines python 
Python :: @foreach 1 numper 
Python :: selenium webdriver without opening browser 
Python :: multiprocessing while applying a function in pandas 
Python :: numpy unique axis 
Python :: async webpage 
Python :: run a for loop in python 
Python :: gfg placement course 
Python :: python Sum of all the factors of a number 
Python :: shape 
Python :: python concatenate dictionaries 
Python :: django change foreign key 
Python :: calendar library in python 
ADD CONTENT
Topic
Content
Source link
Name
2+1 =