Skip to content

Commit 00618e1

Browse files
committed
Add unit test and format code in MaximumSumOfNonAdjacentElements and its test class
1 parent 0fd5e20 commit 00618e1

File tree

2 files changed

+110
-71
lines changed

2 files changed

+110
-71
lines changed

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

+80-71
Original file line numberDiff line numberDiff line change
@@ -8,79 +8,88 @@
88
*/
99
final class MaximumSumOfNonAdjacentElements {
1010

11-
private MaximumSumOfNonAdjacentElements() { }
12-
13-
/**
14-
* Approach 1: Uses a dynamic programming array to store the maximum sum at
15-
* each index. Time Complexity: O(n) - where n is the length of the input
16-
* array. Space Complexity: O(n) - due to the additional dp array.
17-
* @param arr The input array of integers.
18-
* @return The maximum sum of non-adjacent elements.
19-
*/
20-
public static int getMaxSumApproach1(int[] arr) {
21-
int n = arr.length;
22-
int[] dp = new int[n];
23-
24-
// Base case: Maximum sum if only one element is present.
25-
dp[0] = arr[0];
26-
27-
for (int ind = 1; ind < n; ind++) {
28-
29-
// Case 1: Do not take the current element, carry forward the previous max
30-
// sum.
31-
int notTake = dp[ind - 1];
32-
33-
// Case 2: Take the current element, add it to the max sum up to two
34-
// indices before.
35-
int take = arr[ind];
36-
if (ind > 1) {
37-
take += dp[ind - 2];
38-
}
39-
40-
// Store the maximum of both choices in the dp array.
41-
dp[ind] = Math.max(take, notTake);
11+
private MaximumSumOfNonAdjacentElements() {
4212
}
4313

44-
return dp[n - 1];
45-
}
46-
47-
/**
48-
* Approach 2: Optimized space complexity approach using two variables instead
49-
* of an array. Time Complexity: O(n) - where n is the length of the input
50-
* array. Space Complexity: O(1) - as it only uses constant space for two
51-
* variables.
52-
* @param arr The input array of integers.
53-
* @return The maximum sum of non-adjacent elements.
54-
*/
55-
public static int getMaxSumApproach2(int[] arr) {
56-
int n = arr.length;
57-
58-
// Two variables to keep track of previous two results:
59-
// prev1 = max sum up to the last element (n-1)
60-
// prev2 = max sum up to the element before last (n-2)
61-
62-
int prev1 = arr[0]; // Base case: Maximum sum for the first element.
63-
int prev2 = 0;
64-
65-
for (int ind = 1; ind < n; ind++) {
66-
// Case 1: Do not take the current element, keep the last max sum.
67-
int notTake = prev1;
68-
69-
// Case 2: Take the current element and add it to the result from two
70-
// steps back.
71-
int take = arr[ind];
72-
if (ind > 1) {
73-
take += prev2;
74-
}
75-
76-
// Calculate the current maximum sum and update previous values.
77-
int current = Math.max(take, notTake);
78-
79-
// Shift prev1 and prev2 for the next iteration.
80-
prev2 = prev1;
81-
prev1 = current;
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];
8250
}
8351

84-
return prev1;
85-
}
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+
}
8695
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
@Test
10+
public void testGetMaxSumApproach1() {
11+
// Test with various cases
12+
assertEquals(15, MaximumSumOfNonAdjacentElements.getMaxSumApproach1(new int[] {3, 2, 5, 10, 7})); // 3 + 7 + 5
13+
assertEquals(10, MaximumSumOfNonAdjacentElements.getMaxSumApproach1(new int[] {5, 1, 1, 5})); // 5 + 5
14+
assertEquals(0, MaximumSumOfNonAdjacentElements.getMaxSumApproach1(new int[] {})); // Empty array
15+
assertEquals(1, MaximumSumOfNonAdjacentElements.getMaxSumApproach1(new int[] {1})); // Single element
16+
assertEquals(2, MaximumSumOfNonAdjacentElements.getMaxSumApproach1(new int[] {1, 2})); // Take max of both
17+
assertEquals(3, MaximumSumOfNonAdjacentElements.getMaxSumApproach1(new int[] {3, 2})); // Take 3
18+
}
19+
20+
@Test
21+
public void testGetMaxSumApproach2() {
22+
// Test with various cases
23+
assertEquals(15, MaximumSumOfNonAdjacentElements.getMaxSumApproach2(new int[] {3, 2, 5, 10, 7})); // 3 + 7 + 5
24+
assertEquals(10, MaximumSumOfNonAdjacentElements.getMaxSumApproach2(new int[] {5, 1, 1, 5})); // 5 + 5
25+
assertEquals(0, MaximumSumOfNonAdjacentElements.getMaxSumApproach2(new int[] {})); // Empty array
26+
assertEquals(1, MaximumSumOfNonAdjacentElements.getMaxSumApproach2(new int[] {1})); // Single element
27+
assertEquals(2, MaximumSumOfNonAdjacentElements.getMaxSumApproach2(new int[] {1, 2})); // Take max of both
28+
assertEquals(3, MaximumSumOfNonAdjacentElements.getMaxSumApproach2(new int[] {3, 2})); // Take 3
29+
}
30+
}

0 commit comments

Comments
 (0)