Skip to content

Commit ec2ac89

Browse files
committed
feat-changes
1 parent 68fef34 commit ec2ac89

File tree

2 files changed

+35
-22
lines changed

2 files changed

+35
-22
lines changed

src/main/java/com/thealgorithms/dynamicprogramming/MaximumProductSubarray.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public static int maxProduct(int[] nums) {
5050

5151
return maxProduct;
5252
}
53+
}
5354

5455
/**
5556
* Finds the maximum product using a memoization approach with recursion.

src/test/java/com/thealgorithms/dynamicprogramming/MaximumProductSubarrayTest.java

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ void testMixedPositiveAndNegative() {
3232

3333
/**
3434
* Test case for an array containing zeros.
35-
* The expected maximum product is 24 (subarray [2, 3, 4]).
35+
* The expected maximum product is 24 (subarray [4, 6]).
3636
*/
3737
@Test
3838
void testArrayWithZeros() {
@@ -56,7 +56,7 @@ void testSingleElement() {
5656

5757
/**
5858
* Test case for an array with all negative numbers.
59-
* The expected maximum product is the largest single negative number.
59+
* The expected maximum product is 12 (subarray [-3, -4]).
6060
*/
6161
@Test
6262
void testAllNegativeNumbers() {
@@ -90,6 +90,18 @@ void testEmptyArray() {
9090
assertEquals(expected, actual, "The maximum product should be 0 for an empty array.");
9191
}
9292

93+
/**
94+
* Test case for a null array.
95+
* The expected maximum product is 0.
96+
*/
97+
@Test
98+
void testNullArray() {
99+
int[] nums = null;
100+
int expected = 0;
101+
int actual = MaximumProductSubarray.maxProduct(nums);
102+
assertEquals(expected, actual, "The maximum product should be 0 for a null array.");
103+
}
104+
93105
/**
94106
* Test case for an array with alternating positive and negative numbers.
95107
* The expected maximum product is 6 (subarray [2, 3]).
@@ -103,38 +115,38 @@ void testAlternatingNumbers() {
103115
}
104116

105117
/**
106-
* Test case for the memoized version with all positive numbers.
107-
* The expected maximum product is 24.
118+
* Test case for an array with large positive and negative numbers.
119+
* The expected maximum product is 360 (subarray [6, -3, -20]).
108120
*/
109121
@Test
110-
void testMemoizedAllPositiveNumbers() {
111-
int[] nums = {2, 3, 4};
112-
int expected = 24;
113-
int actual = MaximumProductSubarray.maxProductMemoized(nums);
114-
assertEquals(expected, actual, "The maximum product should be 24.");
122+
void testLargeNumbers() {
123+
int[] nums = {6, -3, -20, 0, 5};
124+
int expected = 360;
125+
int actual = MaximumProductSubarray.maxProduct(nums);
126+
assertEquals(expected, actual, "The maximum product should be 360.");
115127
}
116128

117129
/**
118-
* Test case for the memoized version with mixed positive and negative numbers.
119-
* The expected maximum product is 24.
130+
* Test case for an array with single negative number.
131+
* The expected maximum product is the negative number itself.
120132
*/
121133
@Test
122-
void testMemoizedMixedNumbers() {
123-
int[] nums = {2, -3, -4, 1};
124-
int expected = 24;
125-
int actual = MaximumProductSubarray.maxProductMemoized(nums);
126-
assertEquals(expected, actual, "The maximum product should be 24.");
134+
void testSingleNegativeElement() {
135+
int[] nums = {-8};
136+
int expected = -8;
137+
int actual = MaximumProductSubarray.maxProduct(nums);
138+
assertEquals(expected, actual, "The maximum product should be -8.");
127139
}
128140

129141
/**
130-
* Test case for an array with large positive and negative numbers.
131-
* The expected maximum product is 720 (subarray [6, -3, -20]).
142+
* Test case for an array with multiple zeros.
143+
* The expected maximum product is 6 (subarray [2, 3]).
132144
*/
133145
@Test
134-
void testLargeNumbers() {
135-
int[] nums = {6, -3, -20, 0, 5};
136-
int expected = 360;
146+
void testMultipleZeros() {
147+
int[] nums = {0, 2, 3, 0, 4};
148+
int expected = 6;
137149
int actual = MaximumProductSubarray.maxProduct(nums);
138-
assertEquals(expected, actual, "The maximum product should be 360.");
150+
assertEquals(expected, actual, "The maximum product should be 6.");
139151
}
140152
}

0 commit comments

Comments
 (0)