Find the Contiguous Subarray with the Largest Product in a given Array of Integers using JavaScript
Last Updated :
25 Apr, 2024
The objective is to find a contiguous subarray (one in which the elements are adjacent) with the largest product of its elements given an integer array. There are several approaches to finding the contiguous subarray with the largest products using JavaScript which are as follows:
Brute Force Approach
In the brute force method, every possible contiguous subarray is checked out and its product is computed. We compute the product of each subarray after iterating through each element in the array and taking note of all subarrays that begin with that element. In the end, we give back the highest product among all subarrays.
Example: Finding the contiguous subarray with the largest product in an array of integers using the brute force approach in JavaScript.
JavaScript
function maxProductBruteForce(nums) {
let maxProduct = Number.NEGATIVE_INFINITY;
let start = 0;
let end = 0;
for (let i = 0; i < nums.length; i++) {
let product = 1;
for (let j = i; j < nums.length; j++) {
product *= nums[j];
if (product > maxProduct) {
maxProduct = product;
start = i;
end = j;
}
}
}
return {
maxProduct: maxProduct,
subarray: nums.slice(start, end + 1)
};
}
// Example Usage
const nums = [2, 3, -2, 4];
const resultBruteForce =
maxProductBruteForce(nums);
console.log(
"Maximum Product (Brute Force):",
resultBruteForce.maxProduct);
console.log(
"Subarray with Maximum Product (Brute Force):",
resultBruteForce.subarray);
OutputMaximum Product (Brute Force): 6
Subarray with Maximum Product (Brute Force): [ 2, 3 ]
Time Complexity: O(n^2)
Space Complexity: O(1)
Dynamic Programming Approach
The dynamic programming approach tracks `maxProduct` and `minProduct` at each element, updating them with the current element to handle negative numbers and find the largest product subarray. `maxProduct` holds the maximum product ending at the current index, while `minProduct` holds the minimum.
Example: Given an array of integers, find the contiguous subarray with the largest product using an optimized dynamic programming approach in JavaScript.
JavaScript
function maxProductDynamic(nums) {
if (nums.length === 0) return 0;
let maxProduct = nums[0];
let minProduct = nums[0];
let result = nums[0];
let start = 0;
let end = 0;
let tempStart = 0;
for (let i = 1; i < nums.length; i++) {
// Calculate new max and min products
let tempMax = maxProduct;
maxProduct = Math.max(nums[i],
nums[i] * maxProduct,
nums[i] * minProduct);
minProduct = Math
.min(nums[i],
nums[i] * tempMax, nums[i] * minProduct);
// Update result if
// maxProduct is greater
if (maxProduct > result) {
result = maxProduct;
start = tempStart;
end = i;
}
// If current number is 0, reset
// start position for next subarray
if (nums[i] === 0) {
tempStart = i + 1;
}
}
return {
maxProduct: result,
subarray: nums
.slice(start, end + 1)
};
}
const nums = [2, 3, -2, 4];
const resultDynamic =
maxProductDynamic(nums);
console.log(
"Maximum Product (Dynamic Programming):",
resultDynamic.maxProduct);
console.log(
"Subarray with Maximum Product (Dynamic Programming):",
resultDynamic.subarray);
OutputMaximum Product (Dynamic Programming): 6
Subarray with Maximum Product (Dynamic Programming): [ 2, 3 ]
Time Complexity: O(n)
Space Complexity: O(1)
Similar Reads
Find the Maximum Product of Two Integers in the Array using JavaScript ? Given an array of integers, we need to find the maximum product of two integers in the array using JavaScript.Example:Input: arr[] = {4, 6, 8, -5, -4, 4} Output: 48, {6,8} Explanation: Maximum product is 48, which is obtained by multiplying the numbers 6 and 8 of the array.Here are some common appro
8 min read
JavaScript Program to find the Nth smallest/largest element from an unsorted Array In this article, we will see how to find Nth largest and smallest element from an unsorted array. The Nth smallest/largest element from an unsorted array refers to the element that ranks at the Nth position when the array is sorted either in ascending (smallest) or descending (largest) order. It rep
4 min read
JavaScript Program to Find Largest Element in an Array In this article, we are going to learn about the largest element in an array in JavaScript. The largest element in an array refers to the value that holds the greatest numerical or lexicographic (string) order among all elements present in the array. Example: Input : [10, 15, 38, 20, 13];Output: 38H
3 min read
Javascript Program for Third largest element in an array of distinct elements Given an array of n integers, find the third largest element. All the elements in the array are distinct integers. Example : Input: arr[] = {1, 14, 2, 16, 10, 20}Output: The third Largest element is 14Explanation: Largest element is 20, second largest element is 16 and third largest element is 14Inp
6 min read
JavaScript Program to Find kth Largest/Smallest Element in an Array JavaScript allows us to find kth largest/smallest element in an array. We are given an array containing some elements, we have to find kth smallest/largest element from the array where k is a number greater than zero and less than equal to the total number of elements present in the array. There are
5 min read
How to Find Max Sum (i*arr[i]) with Array Rotations in JavaScript? Given an array where rotation operations are allowed, the task is to rotate the array any number of times and find the maximum possible sum of i*arr[i]. Examples : Input: arr = [1, 20, 2, 10] Output: 72 Explanation: We can get 72 by rotating array twice. [2, 10, 1, 20] 20*3 + 1*2 + 10*1 + 2*0 = 72 I
4 min read