1

Applying Kadane algorithm to get max product subarray seems tricky. While I am able to get the max product, I am not really getting the correct range of the max product subarray.

http://www.geeksforgeeks.org/maximum-product-subarray/ explains the way to get the max product but I dont get how we can get the range of the subarray.

Can someone help me understand the range issue? This is a standard interview question and I want to make sure I understand the logic for the product case instead of just saying that the max sum subarray can be modified to answer max product subarray case.

thanks!!

1
  • it is not kadane's algorithm rather is similar to that. Commented Nov 14, 2013 at 0:41

3 Answers 3

1

The link you have provided seems to assume that all the elements are positive. However in my opinion this is not a safe assumption. I have return code to get sub array for maximum product. I have used the same logic used in Kadane's algorithm. Code seems to work for me for all kinds of input. Please let me know if there are issues.

public static int[] getMaxSubArray(int []arr){

    int maxEndingHere = arr[0], maxSoFar = arr[0], startIndex =0, start =0,end=0;

    for(int i=1;i<arr.length;i++){

        if(maxEndingHere<0){
            maxEndingHere = arr[i];
            startIndex = i;         
        }else{          
            maxEndingHere *= arr[i];
        }           
        if(maxEndingHere>=maxSoFar){
            maxSoFar = maxEndingHere;
            start = startIndex;
            end = i;
        }   
    }       
    if(start<=end)
        return Arrays.copyOfRange(arr, start, end+1);

    return null;
}
  1. Sample input = {6, 3, -10, 0, 2} Output = {6,3}
  2. Sample input = {-2,1,-3,4,-1,2,1,-5,4} Output = {4}
  3. Sample input = {-1,-2,-9,-6} Output = {-1}
Sign up to request clarification or add additional context in comments.

1 Comment

Sample input = {-2,1,-3,4,-1,2,1,-5,4} Output = {-2,1,-3,4,-1,2,1,-5,4}
1
def max_subarray(A):
    max_ending_here = max_so_far = 0
    max_start = start = 0
    max_end = end = 0

    # the range is [max_start, max_end)

    for i, x in enumerate(A):
        if max_ending_here + x > 0:
            max_ending_here = max_ending_here + x
            end = i+1
        else:
            max_ending_here = 0
            start = end = i

        if max_ending_here > max_so_far:
            max_so_far = max_ending_here
            max_start = start
            max_end = end

    return (max_start, max_end)

Comments

1

It has basically three cases:

  1. current number is +ve
  2. current number is -ve
  3. current number is 0

You need to have two variable:

  • min which hold minimum value till now

  • max which holds the maximum value till now.

Now for case 3 min and max will be reset to 1.

For case 1: max will be max * a[i] and min will be minimum of min*a[i] and 1.

For case 2: max will be maximum of a[i] * min and 1, but the min value will be max * a[i].

Below is the code:

private static int getMaxProduct(int[] a){
    int minCurrent = 1, maxCurrent = 1, max = Integer.MIN_VALUE;
    for (int current : a) {
        if (current > 0) {
            maxCurrent = maxCurrent * current;
            minCurrent = Math.min(minCurrent * current, 1);
        } else if (current == 0) {
            maxCurrent = 1;
            minCurrent = 1;
        } else {
            int x = maxCurrent;
            maxCurrent = Math.max(minCurrent * current, 1);
            minCurrent = x * current;
        }
        if (max < maxCurrent) {
            max = maxCurrent;
        }
    }
    //System.out.println(minCurrent);
    return max;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.