Given an array of integers, find maximum product subarray. In other words, find a sub-array that has maximum product of its elements.





For example,



Input: { -6, 4, -5, 8, -10, 0, 8 }



Output: The maximum product sub-array is {4, -5, 8, -10} having product 1600





Input: { 40, 0, -20, -10 }



Output: The maximum product sub-array is {-20, -10} having product 200





Naive solution would be to consider every sub-array and find product of their elements. Finally, we return the maximum product found among all sub-arrays. The implementation can be seen here. The time complexity of this solution is O(n2).

A better solution will be to maintain two variables to store the maximum and minimum product ending at current position. Then we traverse the array once and for every index i in the array, we update maximum and minimum product ending at A[i] . We update the result if maximum product ending at any index if more than maximum product found so far.

C #include <stdio.h> // Utility function to find minimum of two numbers int min(int x, int y) { return (x < y) ? x : y; } // Utility function to find maximum of two numbers int max(int x, int y) { return (x > y) ? x : y; } // Function to return maximum product of a sub-array of given array int maxProduct(int arr[], int n) { // maintain two variables to store maximum and minimum product // ending at current index int max_ending = 0, min_ending = 0; // to store maximum product sub-array found so far int max_so_far = 0; // traverse the given array for (int i = 0; i < n; i++) { int temp = max_ending; // update maximum product ending at current index max_ending = max(arr[i], max(arr[i] * max_ending, arr[i] * min_ending)); // update minimum product ending at current index min_ending = min(arr[i], min(arr[i] * temp, arr[i] * min_ending)); max_so_far = max(max_so_far, max_ending); } // return maximum product return max_so_far; } int main(void) { int arr[] = { -6, 4, -5, 8, -10, 0, 8 }; int n = sizeof(arr) / sizeof(arr[0]); printf("The maximum product of a sub-array is %d", maxProduct(arr, n)); return 0; } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 #include <stdio.h> // Utility function to find minimum of two numbers int min ( int x , int y ) { return ( x < y ) ? x : y ; } // Utility function to find maximum of two numbers int max ( int x , int y ) { return ( x > y ) ? x : y ; } // Function to return maximum product of a sub-array of given array int maxProduct ( int arr [ ] , int n ) { // maintain two variables to store maximum and minimum product // ending at current index int max_ending = 0 , min_ending = 0 ; // to store maximum product sub-array found so far int max_so_far = 0 ; // traverse the given array for ( int i = 0 ; i < n ; i ++ ) { int temp = max_ending ; // update maximum product ending at current index max_ending = max ( arr [ i ] , max ( arr [ i ] * max_ending , arr [ i ] * min_ending ) ) ; // update minimum product ending at current index min_ending = min ( arr [ i ] , min ( arr [ i ] * temp , arr [ i ] * min_ending ) ) ; max_so_far = max ( max_so_far , max_ending ) ; } // return maximum product return max_so_far ; } int main ( void ) { int arr [ ] = { - 6 , 4 , - 5 , 8 , - 10 , 0 , 8 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; printf ( "The maximum product of a sub-array is %d" , maxProduct ( arr , n ) ) ; return 0 ; } Download Run Code Output:



The maximum product of a sub-array is 1600

Java class Main { // Function to return maximum product of a sub-array of given array public static int maxProduct(int[] A) { // maintain two variables to store maximum and minimum product // ending at current index int max_ending = 0, min_ending = 0; // to store maximum product sub-array found so far int max_so_far = 0; // traverse the given array for (int i: A) { int temp = max_ending; // update maximum product ending at current index max_ending = Integer.max(i, Integer.max(i * max_ending, i * min_ending) ); // update minimum product ending at current index min_ending = Integer.min(i, Integer.min(i * temp, i * min_ending)); max_so_far = Integer.max(max_so_far, max_ending); } // return maximum product return max_so_far; } public static void main(String[] args) { int[] A = { -6, 4, -5, 8, -10, 0, 8 }; System.out.print("The maximum product of a sub-array is " + maxProduct(A)); } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 class Main { // Function to return maximum product of a sub-array of given array public static int maxProduct ( int [ ] A ) { // maintain two variables to store maximum and minimum product // ending at current index int max_ending = 0 , min_ending = 0 ; // to store maximum product sub-array found so far int max_so_far = 0 ; // traverse the given array for ( int i : A ) { int temp = max_ending ; // update maximum product ending at current index max_ending = Integer . max ( i , Integer . max ( i * max_ending , i * min_ending ) ) ; // update minimum product ending at current index min_ending = Integer . min ( i , Integer . min ( i * temp , i * min_ending ) ) ; max_so_far = Integer . max ( max_so_far , max_ending ) ; } // return maximum product return max_so_far ; } public static void main ( String [ ] args ) { int [ ] A = { - 6 , 4 , - 5 , 8 , - 10 , 0 , 8 } ; System . out . print ( "The maximum product of a sub-array is " + maxProduct ( A ) ) ; } } Download Run Code Output:



The maximum product of a sub-array is 1600

Python # Function to return maximum product of a sublist of given list def maxProduct(A): # maintain two variables to store maximum and minimum product # ending at current index max_ending = min_ending = 0 # to store maximum product sublist found so far max_so_far = 0 # traverse the given list for i in A: temp = max_ending # update maximum product ending at current index max_ending = max(i, max(i * max_ending, i * min_ending)) # update minimum product ending at current index min_ending = min(i, min(i * temp, i * min_ending)) max_so_far = max(max_so_far, max_ending) # return maximum product return max_so_far if __name__ == '__main__': A = [-6, 4, -5, 8, -10, 0, 8] print("The maximum product of a sublist is", maxProduct(A)) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 # Function to return maximum product of a sublist of given list def maxProduct ( A ) : # maintain two variables to store maximum and minimum product # ending at current index max_ending = min_ending = 0 # to store maximum product sublist found so far max_so_far = 0 # traverse the given list for i in A : temp = max _ ending # update maximum product ending at current index max_ending = max ( i , max ( i * max_ending , i * min_ending ) ) # update minimum product ending at current index min_ending = min ( i , min ( i * temp , i * min_ending ) ) max_so_far = max ( max_so_far , max_ending ) # return maximum product return max_so_far if __name__ == '__main__' : A = [ - 6 , 4 , - 5 , 8 , - 10 , 0 , 8 ] print ( "The maximum product of a sublist is" , maxProduct ( A ) ) Download Run Code Output:



The maximum product of a sub-array is 1600







The time complexity of above solution is O(n) and auxiliary space used by the program is O(1).









(33 votes, average: 4.88 out of 5)

Loading...

Thanks for reading.