Skip to content

Commit a1fb1fd

Browse files
authored
Merge branch 'master' into rod_add_tests
2 parents c6b4f7a + 41f7e6a commit a1fb1fd

File tree

8 files changed

+262
-9
lines changed

8 files changed

+262
-9
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ jobs:
1010
uses: actions/setup-java@v4
1111
with:
1212
java-version: 21
13-
distribution: 'adopt'
13+
distribution: 'temurin'
1414
- name: Build with Maven
1515
run: mvn --batch-mode --update-snapshots verify
1616
- name: Upload coverage to codecov (tokenless)

.github/workflows/codeql.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ jobs:
3030
uses: actions/setup-java@v4
3131
with:
3232
java-version: 21
33-
distribution: 'adopt'
33+
distribution: 'temurin'
3434

3535
- name: Initialize CodeQL
3636
uses: github/codeql-action/init@v3

.github/workflows/infer.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818
uses: actions/setup-java@v4
1919
with:
2020
java-version: 21
21-
distribution: 'adopt'
21+
distribution: 'temurin'
2222

2323
- name: Set up OCaml
2424
uses: ocaml/setup-ocaml@v3

DIRECTORY.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@
262262
* [LongestValidParentheses](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/dynamicprogramming/LongestValidParentheses.java)
263263
* [MatrixChainMultiplication](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/dynamicprogramming/MatrixChainMultiplication.java)
264264
* [MatrixChainRecursiveTopDownMemoisation](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/dynamicprogramming/MatrixChainRecursiveTopDownMemoisation.java)
265+
* [MaximumSumOfNonAdjacentElements](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/dynamicprogramming/MaximumSumOfNonAdjacentElements.java)
265266
* [MinimumPathSum](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/dynamicprogramming/MinimumPathSum.java)
266267
* [MinimumSumPartition](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/dynamicprogramming/MinimumSumPartition.java)
267268
* [NewManShanksPrime](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/dynamicprogramming/NewManShanksPrime.java)
@@ -815,8 +816,10 @@
815816
* [LongestPalindromicSubstringTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/LongestPalindromicSubstringTest.java)
816817
* [LongestValidParenthesesTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/LongestValidParenthesesTest.java)
817818
* [MatrixChainMultiplicationTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/MatrixChainMultiplicationTest.java)
819+
* [MaximumSumOfNonAdjacentElementsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/MaximumSumOfNonAdjacentElementsTest.java)
818820
* [MinimumPathSumTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/MinimumPathSumTest.java)
819821
* [MinimumSumPartitionTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/MinimumSumPartitionTest.java)
822+
* [NewManShanksPrimeTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/NewManShanksPrimeTest.java)
820823
* [OptimalJobSchedulingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/OptimalJobSchedulingTest.java)
821824
* [PalindromicPartitioningTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/PalindromicPartitioningTest.java)
822825
* [PartitionProblemTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/PartitionProblemTest.java)
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package com.thealgorithms.dynamicprogramming;
2+
3+
/**
4+
* Class to find the maximum sum of non-adjacent elements in an array. This
5+
* class contains two approaches: one with O(n) space complexity and another
6+
* with O(1) space optimization. For more information, refer to
7+
* https://takeuforward.org/data-structure/maximum-sum-of-non-adjacent-elements-dp-5/
8+
*/
9+
final class MaximumSumOfNonAdjacentElements {
10+
11+
private MaximumSumOfNonAdjacentElements() {
12+
}
13+
14+
/**
15+
* Approach 1: Uses a dynamic programming array to store the maximum sum at
16+
* each index. Time Complexity: O(n) - where n is the length of the input
17+
* array. Space Complexity: O(n) - due to the additional dp array.
18+
* @param arr The input array of integers.
19+
* @return The maximum sum of non-adjacent elements.
20+
*/
21+
public static int getMaxSumApproach1(int[] arr) {
22+
if (arr.length == 0) {
23+
return 0; // Check for empty array
24+
}
25+
26+
int n = arr.length;
27+
int[] dp = new int[n];
28+
29+
// Base case: Maximum sum if only one element is present.
30+
dp[0] = arr[0];
31+
32+
for (int ind = 1; ind < n; ind++) {
33+
34+
// Case 1: Do not take the current element, carry forward the previous max
35+
// sum.
36+
int notTake = dp[ind - 1];
37+
38+
// Case 2: Take the current element, add it to the max sum up to two
39+
// indices before.
40+
int take = arr[ind];
41+
if (ind > 1) {
42+
take += dp[ind - 2];
43+
}
44+
45+
// Store the maximum of both choices in the dp array.
46+
dp[ind] = Math.max(take, notTake);
47+
}
48+
49+
return dp[n - 1];
50+
}
51+
52+
/**
53+
* Approach 2: Optimized space complexity approach using two variables instead
54+
* of an array. Time Complexity: O(n) - where n is the length of the input
55+
* array. Space Complexity: O(1) - as it only uses constant space for two
56+
* variables.
57+
* @param arr The input array of integers.
58+
* @return The maximum sum of non-adjacent elements.
59+
*/
60+
public static int getMaxSumApproach2(int[] arr) {
61+
if (arr.length == 0) {
62+
return 0; // Check for empty array
63+
}
64+
65+
int n = arr.length;
66+
67+
// Two variables to keep track of previous two results:
68+
// prev1 = max sum up to the last element (n-1)
69+
// prev2 = max sum up to the element before last (n-2)
70+
71+
int prev1 = arr[0]; // Base case: Maximum sum for the first element.
72+
int prev2 = 0;
73+
74+
for (int ind = 1; ind < n; ind++) {
75+
// Case 1: Do not take the current element, keep the last max sum.
76+
int notTake = prev1;
77+
78+
// Case 2: Take the current element and add it to the result from two
79+
// steps back.
80+
int take = arr[ind];
81+
if (ind > 1) {
82+
take += prev2;
83+
}
84+
85+
// Calculate the current maximum sum and update previous values.
86+
int current = Math.max(take, notTake);
87+
88+
// Shift prev1 and prev2 for the next iteration.
89+
prev2 = prev1;
90+
prev1 = current;
91+
}
92+
93+
return prev1;
94+
}
95+
}
Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,48 @@
11
package com.thealgorithms.dynamicprogramming;
22

33
/**
4+
* The NewManShanksPrime class provides a method to determine whether the nth
5+
* New Man Shanks prime matches an expected answer.
6+
*
7+
* <p>This is based on the New Man Shanks prime sequence defined by the recurrence
8+
* relation:</p>
9+
*
10+
* <pre>
11+
* a(n) = 2 * a(n-1) + a(n-2) for n >= 2
12+
* a(0) = 1
13+
* a(1) = 1
14+
* </pre>
15+
*
16+
* <p>For more information on New Man Shanks primes, please refer to the
17+
* <a href="https://en.wikipedia.org/wiki/Newman%E2%80%93Shanks%E2%80%93Williams_prime">
18+
* Wikipedia article</a>.</p>
19+
*
20+
* <p>Note: The class is designed to be non-instantiable.</p>
21+
*
422
* @author <a href="https://github.com/siddhant2002">Siddhant Swarup Mallick</a>
5-
* Program description - To find the New Man Shanks Prime.
6-
* <a href="https://en.wikipedia.org/wiki/Newman%E2%80%93Shanks%E2%80%93Williams_prime">Wikipedia</a>
723
*/
824
public final class NewManShanksPrime {
925
private NewManShanksPrime() {
1026
}
1127

28+
/**
29+
* Calculates the nth New Man Shanks prime and checks if it equals the
30+
* expected answer.
31+
*
32+
* @param n the index of the New Man Shanks prime to calculate (0-based).
33+
* @param expectedAnswer the expected value of the nth New Man Shanks prime.
34+
* @return true if the calculated nth New Man Shanks prime matches the
35+
* expected answer; false otherwise.
36+
*/
1237
public static boolean nthManShanksPrime(int n, int expectedAnswer) {
1338
int[] a = new int[n + 1];
14-
// array of n+1 size is initialized
1539
a[0] = 1;
1640
a[1] = 1;
17-
// The 0th and 1st index position values are fixed. They are initialized as 1
41+
1842
for (int i = 2; i <= n; i++) {
1943
a[i] = 2 * a[i - 1] + a[i - 2];
2044
}
21-
// The loop is continued till n
45+
2246
return a[n] == expectedAnswer;
23-
// returns true if calculated answer matches with expected answer
2447
}
2548
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.thealgorithms.dynamicprogramming;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
public class MaximumSumOfNonAdjacentElementsTest {
8+
9+
// Tests for Approach1
10+
@Test
11+
public void testGetMaxSumApproach1WithEmptyArray() {
12+
assertEquals(0, MaximumSumOfNonAdjacentElements.getMaxSumApproach1(new int[] {})); // Empty array
13+
}
14+
15+
@Test
16+
public void testGetMaxSumApproach1WithSingleElement() {
17+
assertEquals(1, MaximumSumOfNonAdjacentElements.getMaxSumApproach1(new int[] {1})); // Single element
18+
}
19+
20+
@Test
21+
public void testGetMaxSumApproach1WithTwoElementsTakeMax() {
22+
assertEquals(2, MaximumSumOfNonAdjacentElements.getMaxSumApproach1(new int[] {1, 2})); // Take max of both
23+
}
24+
25+
@Test
26+
public void testGetMaxSumApproach1WithMultipleElements() {
27+
assertEquals(15, MaximumSumOfNonAdjacentElements.getMaxSumApproach1(new int[] {3, 2, 5, 10, 7})); // 3 + 7 + 5
28+
assertEquals(10, MaximumSumOfNonAdjacentElements.getMaxSumApproach1(new int[] {5, 1, 1, 5})); // 5 + 5
29+
}
30+
31+
// Tests for Approach2
32+
@Test
33+
public void testGetMaxSumApproach2WithEmptyArray() {
34+
assertEquals(0, MaximumSumOfNonAdjacentElements.getMaxSumApproach2(new int[] {})); // Empty array
35+
}
36+
37+
@Test
38+
public void testGetMaxSumApproach2WithSingleElement() {
39+
assertEquals(1, MaximumSumOfNonAdjacentElements.getMaxSumApproach2(new int[] {1})); // Single element
40+
}
41+
42+
@Test
43+
public void testGetMaxSumApproach2WithTwoElementsTakeMax() {
44+
assertEquals(2, MaximumSumOfNonAdjacentElements.getMaxSumApproach2(new int[] {1, 2})); // Take max of both
45+
}
46+
47+
@Test
48+
public void testGetMaxSumApproach2WithMultipleElements() {
49+
assertEquals(15, MaximumSumOfNonAdjacentElements.getMaxSumApproach2(new int[] {3, 2, 5, 10, 7})); // 3 + 7 + 5
50+
assertEquals(10, MaximumSumOfNonAdjacentElements.getMaxSumApproach2(new int[] {5, 1, 1, 5})); // 5 + 5
51+
}
52+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package com.thealgorithms.dynamicprogramming;
2+
3+
import static org.junit.jupiter.api.Assertions.assertFalse;
4+
import static org.junit.jupiter.api.Assertions.assertTrue;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
/**
9+
* Unit tests for the NewManShanksPrime class.
10+
* This test class verifies the correctness of the nthManShanksPrime method
11+
* for various input cases.
12+
*/
13+
class NewManShanksPrimeTest {
14+
15+
/**
16+
* Test case for the 1st New Man Shanks prime.
17+
* The expected answer is 1.
18+
*/
19+
@Test
20+
void testNthManShanksPrime1() {
21+
int n = 1;
22+
int expectedAnswer = 1;
23+
assertTrue(NewManShanksPrime.nthManShanksPrime(n, expectedAnswer), "The 1st New Man Shanks prime should be 1.");
24+
}
25+
26+
/**
27+
* Test case for the 2nd New Man Shanks prime.
28+
* The expected answer is 3.
29+
*/
30+
@Test
31+
void testNthManShanksPrime2() {
32+
int n = 2;
33+
int expectedAnswer = 3;
34+
assertTrue(NewManShanksPrime.nthManShanksPrime(n, expectedAnswer), "The 2nd New Man Shanks prime should be 3.");
35+
}
36+
37+
/**
38+
* Test case for the 3rd New Man Shanks prime.
39+
* The expected answer is 7.
40+
*/
41+
@Test
42+
void testNthManShanksPrime3() {
43+
int n = 3;
44+
int expectedAnswer = 7;
45+
assertTrue(NewManShanksPrime.nthManShanksPrime(n, expectedAnswer), "The 3rd New Man Shanks prime should be 7.");
46+
}
47+
48+
/**
49+
* Test case for the 4th New Man Shanks prime.
50+
* The expected answer is 17.
51+
*/
52+
@Test
53+
void testNthManShanksPrime4() {
54+
int n = 4;
55+
int expectedAnswer = 17;
56+
assertTrue(NewManShanksPrime.nthManShanksPrime(n, expectedAnswer), "The 4th New Man Shanks prime should be 17.");
57+
}
58+
59+
/**
60+
* Test case for the 5th New Man Shanks prime.
61+
* The expected answer is 41.
62+
*/
63+
@Test
64+
void testNthManShanksPrime5() {
65+
int n = 5;
66+
int expectedAnswer = 41;
67+
assertTrue(NewManShanksPrime.nthManShanksPrime(n, expectedAnswer), "The 5th New Man Shanks prime should be 41.");
68+
}
69+
70+
/**
71+
* Test case with an incorrect expected answer.
72+
* For n = 2, the expected answer is 3.
73+
*/
74+
@Test
75+
void testNthManShanksPrimeIncorrectAnswer() {
76+
int n = 2;
77+
int expectedAnswer = 4; // Incorrect expected value
78+
assertFalse(NewManShanksPrime.nthManShanksPrime(n, expectedAnswer), "The 2nd New Man Shanks prime should not be 4.");
79+
}
80+
}

0 commit comments

Comments
 (0)