Given an array arr[] consisting of positive, negative, and zero values, find the maximum product that can be obtained from any contiguous subarray of arr[].
Examples:
Input: arr[] = [-2, 6, -3, -10, 0, 2]
Output: 180
Explanation: The subarray with maximum product is [6, -3, -10] with product = 6 * (-3) * (-10) = 180.
Input: arr[] = [-1, -3, -10, 0, 6]
Output: 30
Explanation: The subarray with maximum product is [-3, -10] with product = (-3) * (-10) = 30.
Input: arr[] = [2, 3, 4]
Output: 24
Explanation: For an array with all positive elements, the result is product of all elements.
[Naive Approach] Using Two Nested Loops – O(n^2) Time and O(1) Space
The idea is to traverse over every contiguous subarray, find the product of each of these subarrays and return the maximum product among all the subarrays.
C++
#include <iostream>
#include<vector>
using namespace std;
int maxProduct(vector<int> &arr) {
int n = arr.size();
// Initializing result
int maxProd = arr[0];
for (int i = 0; i < n; i++) {
int mul = 1;
// traversing in current subarray
for (int j = i; j < n; j++) {
mul *= arr[j];
// updating result every time
// to keep track of the maximum product
maxProd = max(maxProd, mul);
}
}
return maxProd;
}
int main() {
vector<int> arr = { -2, 6, -3, -10, 0, 2 };
cout << maxProduct(arr);
return 0;
}
C
#include <stdio.h>
int maxProduct(int arr[], int n) {
// Initializing result
int maxProd = arr[0];
for (int i = 0; i < n; i++) {
int mul = 1;
// traversing in current subarray
for (int j = i; j < n; j++) {
mul *= arr[j];
// updating result every time
// to keep track of the maximum product
maxProd = (maxProd > mul) ? maxProd : mul;
}
}
return maxProd;
}
int main() {
int arr[] = { -2, 6, -3, -10, 0, 2 };
int n = sizeof(arr) / sizeof(arr[0]);
printf("%lld\n", maxProduct(arr, n));
return 0;
}
Java
class GfG {
static int maxProduct(int arr[]) {
int n = arr.length;
// Initializing result
int maxProd = arr[0];
for (int i = 0; i < n; i++) {
int mul = 1;
// traversing in current subarray
for (int j = i; j < n; j++) {
mul *= arr[j];
// updating result every time
// to keep track of the maximum product
maxProd = Math.max(maxProd, mul);
}
}
return maxProd;
}
public static void main(String[] args) {
int arr[] = { -2, 6, -3, -10, 0, 2 };
System.out.println(maxProduct(arr));
}
}
Python
def maxProduct(arr):
n = len(arr)
# Initializing result
maxProd = arr[0]
for i in range(n):
mul = 1
# traversing in current subarray
for j in range(i, n):
mul *= arr[j]
# updating result every time
# to keep track of the maximum product
maxProd = max(maxProd, mul)
return maxProd
if __name__ == "__main__":
arr = [-2, 6, -3, -10, 0, 2]
print(maxProduct(arr))
C#
using System;
class GfG {
static int maxProduct(int[] arr) {
int n = arr.Length;
// Initializing result
int maxProd = arr[0];
for (int i = 0; i < n; i++) {
int mul = 1;
// traversing in current subarray
for (int j = i; j < n; j++) {
mul *= arr[j];
// updating result every time
// to keep track of the maximum product
maxProd = Math.Max(maxProd, mul);
}
}
return maxProd;
}
public static void Main(String[] args) {
int[] arr = { -2, 6, -3, -10, 0, 2 };
Console.Write(maxProduct(arr));
}
}
JavaScript
function maxProduct(arr) {
const n = arr.length;
// Initializing result
let maxProd = arr[0];
for (let i = 0; i < n; i++) {
let mul = 1;
// Traversing in current subarray
for (let j = i; j < n; j++) {
mul *= arr[j];
// Update result to keep track of the maximum product
if(mul > maxProd)
maxProd = mul;
}
}
return maxProd;
}
// Driver Code
const arr = [-2, 6, -3, -10, 0, 2];
console.log(maxProduct(arr).toString());
[Expected Approach - 1] Greedy Min-Max Product - O(n) Time and O(1) Space
Let's assume that the input array has only positive elements. Then, we can simply iterate from left to right keeping track of the maximum running product ending at any index. The maximum product would be the product ending at the last index. The problem arises when we encounter zero or a negative element.
If we encounter zero, then all the subarrays containing this zero will have product = 0, so zero simply resets the product of the subarray.
If we encounter a negative number, we need to keep track of the minimum product as well as the maximum product ending at the previous index. This is because when we multiply the minimum product with a negative number, it can give us the maximum product. So, keeping track of minimum product ending at any index is important as it can lead to the maximum product on encountering a negative number.
Step By Step Approach:
- Initialize three variables: currMax, currMin, and maxProd with the first element of the array.
- Loop through the array from index 1 to end to evaluate every position’s contribution.
- For each index, calculate the temporary maximum using max of current value, current × currMax, and current × currMin.
- Update currMin using min of current value, current × currMax, and current × currMin.
- Assign currMax to the previously computed temporary maximum value.
- Update maxProd by comparing it with the updated currMax value.
- Finally, return maxProd which stores the maximum product of any subarray.
Working:
C++
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int maxProduct(vector<int> &arr) {
int n = arr.size();
// max product ending at the current index
int currMax = arr[0];
// min product ending at the current index
int currMin = arr[0];
// Initialize overall max product
int maxProd = arr[0];
// Iterate through the array
for (int i = 1; i < n; i++) {
// Temporary variable to store the maximum product ending
// at the current index
int temp = max({ arr[i], arr[i] * currMax,
arr[i] * currMin });
// Update the minimum product ending at the current index
currMin = min({ arr[i], arr[i] * currMax,
arr[i] * currMin });
// Update the maximum product ending at the current index
currMax = temp;
// Update the overall maximum product
maxProd = max(maxProd, currMax);
}
return maxProd;
}
int main() {
vector<int> arr = { -2, 6, -3, -10, 0, 2 };
cout << maxProduct(arr);
return 0;
}
C
#include <stdio.h>
#include <limits.h>
int max(int a, int b, int c) {
if (a >= b && a >= c) return a;
if (b >= a && b >= c) return b;
return c;
}
int min(int a, int b, int c) {
if (a <= b && a <= c) return a;
if (b <= a && b <= c) return b;
return c;
}
int maxProduct(int arr[], int n) {
// max product ending at the current index
int currMax = arr[0];
// min product ending at the current index
int currMin = arr[0];
// Initialize overall max product
int maxProd = arr[0];
// Iterate through the array
for (int i = 1; i < n; i++) {
// Temporary variable to store the maximum product ending at the
// current index
int temp = max(arr[i], arr[i] * currMax, arr[i] * currMin);
// Update the minimum product ending at the current index
currMin = min(arr[i], arr[i] * currMax, arr[i] * currMin);
// Update the maximum product ending at the current index
currMax = temp;
// Update the overall maximum product
maxProd = max(maxProd, currMax, maxProd);
}
return maxProd;
}
int main() {
int arr[] = { -2, 6, -3, -10, 0, 2 };
int n = sizeof(arr) / sizeof(arr[0]);
printf("%lld\n", maxProduct(arr, n));
return 0;
}
Java
class GfG {
static int max(int a, int b, int c) {
return Math.max(a, Math.max(b, c));
}
static int min(int a, int b, int c) {
return Math.min(a, Math.min(b, c));
}
static int maxProduct(int[] arr) {
int n = arr.length;
// max product ending at the current index
int currMax = arr[0];
// min product ending at the current index
int currMin = arr[0];
// Initialize overall max product
int maxProd = arr[0];
// Iterate through the array
for (int i = 1; i < n; i++) {
// Temporary variable to store the maximum product ending
// at the current index
int temp = max(arr[i], arr[i] * currMax, arr[i] * currMin);
// Update the minimum product ending at the current index
currMin = min(arr[i], arr[i] * currMax, arr[i] * currMin);
// Update the maximum product ending at the current index
currMax = temp;
// Update the overall maximum product
maxProd = Math.max(maxProd, currMax);
}
return maxProd;
}
public static void main(String[] args) {
int[] arr = { -2, 6, -3, -10, 0, 2 };
System.out.println(maxProduct(arr));
}
}
Python
def maxProduct(arr):
n = len(arr)
# max product ending at the current index
currMax = arr[0]
# min product ending at the current index
currMin = arr[0]
# Initialize overall max product
maxProd = arr[0]
# Iterate through the array
for i in range(1, n):
# Temporary variable to store the maximum product ending
# at the current index
temp = max(arr[i], arr[i] * currMax, arr[i] * currMin)
# Update the minimum product ending at the current index
currMin = min(arr[i], arr[i] * currMax, arr[i] * currMin)
# Update the maximum product ending at the current index
currMax = temp
# Update the overall maximum product
maxProd = max(maxProd, currMax)
return maxProd
if __name__ == "__main__":
arr = [-2, 6, -3, -10, 0, 2]
print(maxProduct(arr))
C#
using System;
class GfG {
static int maxProduct(int[] arr) {
int n = arr.Length;
// max product ending at the current index
int currMax = arr[0];
// min product ending at the current index
int currMin = arr[0];
// Initialize overall max product
int maxProd = arr[0];
// Iterate through the array
for (int i = 1; i < n; i++) {
// Temporary variable to store the maximum product ending
// at the current index
int temp = Math.Max(arr[i], Math.Max(arr[i] * currMax,
arr[i] * currMin));
// Update the minimum product ending at the current index
currMin = Math.Min(arr[i], Math.Min(arr[i] * currMax,
arr[i] * currMin));
// Update the maximum product ending at the current index
currMax = temp;
// Update the overall maximum product
maxProd = Math.Max(maxProd, currMax);
}
return maxProd;
}
static void Main() {
int[] arr = { -2, 6, -3, -10, 0, 2 };
Console.WriteLine(maxProduct(arr));
}
}
JavaScript
function max(a, b, c) {
return a > b ? (a > c ? a : c) : (b > c ? b : c);
}
function min(a, b, c) {
return a < b ? (a < c ? a : c) : (b < c ? b : c);
}
function maxProduct(arr) {
// Initialize the maximum and minimum products ending at
// the current index
let currMax = arr[0];
let currMin = arr[0];
// Initialize the overall maximum product
let maxProd = arr[0];
// Iterate through the array starting from the second element
for (let i = 1; i < arr.length; i++) {
// Calculate potential maximum product at this index
const temp = max(arr[i] * currMax, arr[i] * currMin, arr[i]);
// Update the minimum product ending at the current index
currMin = min(arr[i] * currMax, arr[i] * currMin, arr[i]);
// Update the maximum product ending at the current index
currMax = temp;
// Update the overall maximum product
maxProd = max(maxProd, maxProd, currMax);
}
return maxProd;
}
// Driver Code
const arr = [ -2, 6, -3, -10, 0, 2 ];
console.log(maxProduct(arr).toString());
[Expected Approach - 2] By Traversing in Both Directions - O(n) Time and O(1) Space
We will follow a simple approach that is to traverse from the start and keep track of the running product and if the running product is greater than the max product, then we update the max product. Also, if we encounter '0' then make product of all elements till now equal to 1 because from the next element, we will start a new subarray.
But what is the problem with this approach?
Problem will occur when our array will contain odd no. of negative elements. In that case, we have to reject one negative element so that we can even no. of negative elements and their product can be positive. Now, since subarray should be contiguous so we can't simply reject any one negative element. We have to either reject the first negative element or the last negative element.
Now, if we traverse from start then only the last negative element can be rejected and if we traverse from the last then the first negative element can be rejected. So we will traverse from both ends and find the maximum product subarray.
Illustration:
C++
#include <iostream>
#include <vector>
#include <climits>
#include <algorithm>
using namespace std;
int maxProduct(vector<int> &arr) {
int n = arr.size();
int maxProd = INT_MIN;
// leftToRight to store product from left to Right
int leftToRight = 1;
// rightToLeft to store product from right to left
int rightToLeft = 1;
for (int i = 0; i < n; i++) {
if (leftToRight == 0)
leftToRight = 1;
if (rightToLeft == 0)
rightToLeft = 1;
// calculate product from index left to right
leftToRight *= arr[i];
// calculate product from index right to left
int j = n - i - 1;
rightToLeft *= arr[j];
maxProd = max({leftToRight, rightToLeft, maxProd});
}
return maxProd;
}
int main() {
vector<int> arr = { -2, 6, -3, -10, 0, 2 };
cout << maxProduct(arr);
return 0;
}
C
#include <stdio.h>
#include <limits.h>
int maxProduct(int arr[], int n) {
int maxProd = LLONG_MIN;
// leftToRight to store product from left to Right
int leftToRight = 1;
// rightToLeft to store product from right to left
int rightToLeft = 1;
for (int i = 0; i < n; i++) {
if (leftToRight == 0)
leftToRight = 1;
if (rightToLeft == 0)
rightToLeft = 1;
// calculate product from index left to right
leftToRight *= arr[i];
// calculate product from index right to left
int j = n - i - 1;
rightToLeft *= arr[j];
maxProd = (leftToRight > maxProd ? leftToRight : maxProd);
maxProd = (rightToLeft > maxProd ? rightToLeft : maxProd);
}
return maxProd;
}
int main() {
int arr[] = { -2, 6, -3, -10, 0, 2 };
int n = sizeof(arr) / sizeof(arr[0]);
printf("%lld\n", maxProduct(arr, n));
return 0;
}
Java
class GfG {
static int maxProduct(int[] arr) {
int n = arr.length;
int maxProd = Integer.MIN_VALUE;
// leftToRight to store product from left to Right
int leftToRight = 1;
// rightToLeft to store product from right to left
int rightToLeft = 1;
for (int i = 0; i < n; i++) {
if (leftToRight == 0)
leftToRight = 1;
if (rightToLeft == 0)
rightToLeft = 1;
// calculate product from index left to right
leftToRight *= arr[i];
// calculate product from index right to left
int j = n - i - 1;
rightToLeft *= arr[j];
maxProd = Math.max(leftToRight,
Math.max(rightToLeft, maxProd));
}
return maxProd;
}
public static void main(String[] args) {
int[] arr = { -2, 6, -3, -10, 0, 2 };
System.out.println(maxProduct(arr));
}
}
Python
def maxProduct(arr):
n = len(arr)
maxProd = float('-inf')
# leftToRight to store product from left to Right
leftToRight = 1
# rightToLeft to store product from right to left
rightToLeft = 1
for i in range(n):
if leftToRight == 0:
leftToRight = 1
if rightToLeft == 0:
rightToLeft = 1
# calculate product from index left to right
leftToRight *= arr[i]
# calculate product from index right to left
j = n - i - 1
rightToLeft *= arr[j]
maxProd = max(leftToRight, rightToLeft, maxProd)
return maxProd
if __name__=="__main__":
arr = [-2, 6, -3, -10, 0, 2]
print(maxProduct(arr))
C#
using System;
class GfG {
static int maxProduct(int[] arr) {
int n = arr.Length;
int maxProd = int.MinValue;
// leftToRight to store product from left to Right
int leftToRight = 1;
// rightToLeft to store product from right to left
int rightToLeft = 1;
for (int i = 0; i < n; i++) {
if (leftToRight == 0)
leftToRight = 1;
if (rightToLeft == 0)
rightToLeft = 1;
// calculate product from index left to right
leftToRight *= arr[i];
// calculate product from index right to left
int j = n - i - 1;
rightToLeft *= arr[j];
maxProd = Math.Max(leftToRight, Math.Max(rightToLeft, maxProd));
}
return maxProd;
}
static void Main() {
int[] arr = { -2, 6, -3, -10, 0, 2 };
Console.WriteLine(maxProduct(arr));
}
}
JavaScript
function maxProduct(arr) {
let n = arr.length;
let maxProd = Number.MIN_SAFE_INTEGER;
// leftToRight to store product from left to Right
let leftToRight = 1;
// rightToLeft to store product from right to left
let rightToLeft = 1;
for (let i = 0; i < n; i++) {
if (leftToRight === 0)
leftToRight = 1;
if (rightToLeft === 0)
rightToLeft = 1;
// calculate product from index left to right
leftToRight *= arr[i];
// calculate product from index right to left
let j = n - i - 1;
rightToLeft *= arr[j];
maxProd = Math.max(leftToRight, rightToLeft, maxProd);
}
return maxProd;
}
// Driver Code
let arr = [ -2, 6, -3, -10, 0, 2 ];
console.log(maxProduct(arr));
Similar Reads
Basics & Prerequisites
Data Structures
Getting Started with Array Data StructureArray is a collection of items of the same variable type that are stored at contiguous memory locations. It is one of the most popular and simple data structures used in programming. Basic terminologies of ArrayArray Index: In an array, elements are identified by their indexes. Array index starts fr
14 min read
String in Data StructureA string is a sequence of characters. The following facts make string an interesting data structure.Small set of elements. Unlike normal array, strings typically have smaller set of items. For example, lowercase English alphabet has only 26 characters. ASCII has only 256 characters.Strings are immut
2 min read
Hashing in Data StructureHashing is a technique used in data structures that efficiently stores and retrieves data in a way that allows for quick access. Hashing involves mapping data to a specific index in a hash table (an array of items) using a hash function. It enables fast retrieval of information based on its key. The
2 min read
Linked List Data StructureA linked list is a fundamental data structure in computer science. It mainly allows efficient insertion and deletion operations compared to arrays. Like arrays, it is also used to implement other data structures like stack, queue and deque. Hereâs the comparison of Linked List vs Arrays Linked List:
2 min read
Stack Data StructureA Stack is a linear data structure that follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). LIFO implies that the element that is inserted last, comes out first and FILO implies that the element that is inserted first
2 min read
Queue Data StructureA Queue Data Structure is a fundamental concept in computer science used for storing and managing data in a specific order. It follows the principle of "First in, First out" (FIFO), where the first element added to the queue is the first one to be removed. It is used as a buffer in computer systems
2 min read
Tree Data StructureTree Data Structure is a non-linear data structure in which a collection of elements known as nodes are connected to each other via edges such that there exists exactly one path between any two nodes. Types of TreeBinary Tree : Every node has at most two childrenTernary Tree : Every node has at most
4 min read
Graph Data StructureGraph Data Structure is a collection of nodes connected by edges. It's used to represent relationships between different entities. If you are looking for topic-wise list of problems on different topics like DFS, BFS, Topological Sort, Shortest Path, etc., please refer to Graph Algorithms. Basics of
3 min read
Trie Data StructureThe Trie data structure is a tree-like structure used for storing a dynamic set of strings. It allows for efficient retrieval and storage of keys, making it highly effective in handling large datasets. Trie supports operations such as insertion, search, deletion of keys, and prefix searches. In this
15+ min read
Algorithms
Searching AlgorithmsSearching algorithms are essential tools in computer science used to locate specific items within a collection of data. In this tutorial, we are mainly going to focus upon searching in an array. When we search an item in an array, there are two most common algorithms used based on the type of input
2 min read
Sorting AlgorithmsA Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order. There exist different sorting algorithms for differ
3 min read
Introduction to RecursionThe process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. A recursive algorithm takes one step toward solution and then recursively call itself to further move. The algorithm stops once we reach the solution
14 min read
Greedy AlgorithmsGreedy algorithms are a class of algorithms that make locally optimal choices at each step with the hope of finding a global optimum solution. At every step of the algorithm, we make a choice that looks the best at the moment. To make the choice, we sometimes sort the array so that we can always get
3 min read
Graph AlgorithmsGraph is a non-linear data structure like tree data structure. The limitation of tree is, it can only represent hierarchical data. For situations where nodes or vertices are randomly connected with each other other, we use Graph. Example situations where we use graph data structure are, a social net
3 min read
Dynamic Programming or DPDynamic Programming is an algorithmic technique with the following properties.It is mainly an optimization over plain recursion. Wherever we see a recursive solution that has repeated calls for the same inputs, we can optimize it using Dynamic Programming. The idea is to simply store the results of
3 min read
Bitwise AlgorithmsBitwise algorithms in Data Structures and Algorithms (DSA) involve manipulating individual bits of binary representations of numbers to perform operations efficiently. These algorithms utilize bitwise operators like AND, OR, XOR, NOT, Left Shift, and Right Shift.BasicsIntroduction to Bitwise Algorit
4 min read
Advanced
Segment TreeSegment Tree is a data structure that allows efficient querying and updating of intervals or segments of an array. It is particularly useful for problems involving range queries, such as finding the sum, minimum, maximum, or any other operation over a specific range of elements in an array. The tree
3 min read
Pattern SearchingPattern searching algorithms are essential tools in computer science and data processing. These algorithms are designed to efficiently find a particular pattern within a larger set of data. Patten SearchingImportant Pattern Searching Algorithms:Naive String Matching : A Simple Algorithm that works i
2 min read
GeometryGeometry is a branch of mathematics that studies the properties, measurements, and relationships of points, lines, angles, surfaces, and solids. From basic lines and angles to complex structures, it helps us understand the world around us.Geometry for Students and BeginnersThis section covers key br
2 min read
Interview Preparation
Practice Problem