Skip to content

Commit e089e45

Browse files
committed
Update maximum product subarray problem
1 parent 434e9bc commit e089e45

File tree

5 files changed

+100
-10
lines changed

5 files changed

+100
-10
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ List of Programs related to data structures and algorithms
5454
7. Sort Colors: [Source](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/7.sortColors/sortColors.js)
5555
[Playground](https://livecodes.io/?console=open&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/7.sortColors/sortColors.js) [Documentation](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/7.sortColors/sortColors.md)
5656

57-
8. Maximum product subarray: [JavaScript](https://livecodes.io/?console=open&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/maxProductSubArray.js)
57+
8. Maximum product subarray: [Source](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/8.maxProductSubarray/maxProductSubarray.js)
58+
[Playground](https://livecodes.io/?console=open&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/8.maxProductSubarray/maxProductSubarray.js) [Documentation](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/8.maxProductSubarray/maxProductSubarray.md)
5859

5960
9. Find minimum in rotated sorted array: [JavaScript](https://livecodes.io/?console=open&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/minRotatedSortedArray.js)
6061

Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
public class MaxProductSubArray {
1+
package maxProductSubarray;
2+
3+
public class MaxProductSubarray {
24
private static int maxProductSubArray(int[] nums) {
35
int result = nums[0];
46
int currentMax = 1, currentMin = 1;
5-
for(int n: nums) {
6-
int tempMaxProduct = currentMax * n;
7-
currentMax = Math.max(Math.max(tempMaxProduct, currentMin * n), n);
8-
currentMin = Math.min(Math.min(tempMaxProduct, currentMin * n), n);
7+
for(int num: nums) {
8+
int tempMaxProduct = currentMax * num;
9+
int tempMinProduct = currentMin * num;
10+
currentMax = Math.max(Math.max(tempMaxProduct, tempMinProduct), num);
11+
currentMin = Math.min(Math.min(tempMaxProduct, tempMinProduct), num);
912
result = Math.max(currentMax, result);
1013
}
1114
return result;
@@ -17,5 +20,8 @@ public static void main(String[] args) {
1720

1821
int[] numbers2 = {2, 7,-4, 0, 3, 3};
1922
System.out.println(maxProductSubArray(numbers2));
23+
24+
int[] numbers3 = {-2, 0, -2};
25+
System.out.println(maxProductSubArray(numbers3));
2026
}
2127
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
**Description:**
2+
Given an integer array `nums` that contains both positive and negative integers, find a contiguous subarray that has the largest product, and return the product.
3+
4+
### Examples
5+
**Example 1:**
6+
Input: nums = [6, 7,-4, 5, 8, ]
7+
Output: 6
8+
9+
Example 2:
10+
Input: nums = [-2,0,-2]
11+
Output: 0
12+
13+
**Algorithmic Steps:**
14+
15+
This problem is solved with the help of Kadane's algorithm(dynamic programming technique). The algorithmic approach can be summarized as follows:
16+
17+
1. Initialize the maximum product subarray(`result`) with a first number.
18+
19+
**Note:** It is not suggested to assign the value to either 0 or 1 because the maximum value can be less than or equal to 0.
20+
21+
2. Initialize two variables named `currentMax` and `currentMin` to `1`. They are used to keep track of the running maximum and minimum products.
22+
23+
3. Iterate over the entire input array using for-each loop
24+
25+
4. Find the temporary maximum and minimum products for each current element.
26+
27+
5. Calculate the maximum value between maximum and minimum values of step4, and this calculated maximum result needs to be compared with current element to find the final current maximum product.
28+
29+
**Note:** Since an array contain both positive and negative integers, the product of current element with minimum product calculated so far may result into maximum value. That is the reason why we took maximum of current maximum and current minimum proucts.
30+
31+
6. Calculate the minimum value between maximum and minimum values of step4, and this calculated minimum result needs to be compared with current element to find the final current minimum product. This current minimum product is useful to calculate the maximum value for the next iteration.
32+
33+
7. Update the global maximum product by taking the maximum of current maximum product and global maximum product of previous iteration.
34+
35+
8. Return the global maximum `result` after the end of for-each iteration.
36+
37+
**Time and Space complexity:**
38+
39+
This algorithm has a time complexity of `O(n)`, where `n` is the number of elements. This is because we iterate the array at most once.
40+
41+
Here, we don't use any additional datastructure other than the two sum variables. Hence, the space complexity will be O(1).

src/javascript/algorithms/array/maxProductSubArray.js renamed to src/javascript/algorithms/array/8.maxProductSubarray/maxProductSubarray.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ function maxProductSubArray(nums) {
22
let result = nums[0];
33
let currentMax = currentMin = 1;
44

5-
for(let n of nums) {
6-
let tempMaxProduct = currentMax * n;
7-
currentMax = Math.max(Math.max(tempMaxProduct, currentMin *n), n);
8-
currentMin = Math.min(Math.min(tempMaxProduct, currentMin *n), n);
5+
for(let num of nums) {
6+
let tempMaxProduct = currentMax * num;
7+
let tempMinProduct = currentMin * num;
8+
currentMax = Math.max(Math.max(tempMaxProduct, tempMinProduct), num);
9+
currentMin = Math.min(Math.min(tempMaxProduct, tempMinProduct), num);
910
result = Math.max(result, currentMax);
1011
}
1112
return result;
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
**Description:**
2+
Given an integer array `nums` that contains both positive and negative integers, find a contiguous subarray that has the largest product, and return the product.
3+
4+
### Examples
5+
**Example 1:**
6+
Input: nums = [6, 7,-4, 5, 8, ]
7+
Output: 6
8+
9+
Example 2:
10+
Input: nums = [-2,0,-2]
11+
Output: 0
12+
13+
**Algorithmic Steps:**
14+
15+
This problem is solved with the help of Kadane's algorithm(dynamic programming technique). The algorithmic approach can be summarized as follows:
16+
17+
1. Initialize the maximum product subarray(`result`) with a first number.
18+
19+
**Note:** It is not suggested to assign the value to either 0 or 1 because the maximum value can be less than or equal to 0.
20+
21+
2. Initialize two variables named `currentMax` and `currentMin` to `1`. They are used to keep track of the running maximum and minimum products.
22+
23+
3. Iterate over the entire input array using for-each loop
24+
25+
4. Find the temporary maximum and minimum products for each current element.
26+
27+
5. Calculate the maximum value between maximum and minimum values of step4, and this calculated maximum result needs to be compared with current element to find the final current maximum product.
28+
29+
**Note:** Since an array contain both positive and negative integers, the product of current element with minimum product calculated so far may result into maximum value. That is the reason why we took maximum of current maximum and current minimum proucts.
30+
31+
6. Calculate the minimum value between maximum and minimum values of step4, and this calculated minimum result needs to be compared with current element to find the final current minimum product. This current minimum product is useful to calculate the maximum value for the next iteration.
32+
33+
7. Update the global maximum product by taking the maximum of current maximum product and global maximum product of previous iteration.
34+
35+
8. Return the global maximum `result` after the end of for-each iteration.
36+
37+
**Time and Space complexity:**
38+
39+
This algorithm has a time complexity of `O(n)`, where `n` is the number of elements. This is because we iterate the array at most once.
40+
41+
Here, we don't use any additional datastructure other than the two sum variables. Hence, the space complexity will be O(1).

0 commit comments

Comments
 (0)