7
7
* https://takeuforward.org/data-structure/maximum-sum-of-non-adjacent-elements-dp-5/
8
8
*/
9
9
final class MaximumSumOfNonAdjacentElements {
10
- private MaximumSumOfNonAdjacentElements () {
11
- }
12
-
13
- /**
14
- * Approach 1: Uses a dynamic programming array to store the maximum sum at each
15
- * index. Time Complexity: O(n) - where n is the length of the input array.
16
- * 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 sum.
30
- int notTake = dp [ind - 1 ];
31
-
32
- // Case 2: Take the current element, add it to the max sum up to two indices
33
- // before.
34
- int take = arr [ind ];
35
- if (ind > 1 ) {
36
- take += dp [ind - 2 ];
37
- }
38
-
39
- // Store the maximum of both choices in the dp array.
40
- dp [ind ] = Math .max (take , notTake );
41
- }
42
-
43
- return dp [n - 1 ];
44
- }
45
-
46
- /**
47
- * Approach 2: Optimized space complexity approach using two variables instead
48
- * of an array. Time Complexity: O(n) - where n is the length of the input
49
- * array. Space Complexity: O(1) - as it only uses constant space for two
50
- * variables.
51
- * @param arr The input array of integers.
52
- * @return The maximum sum of non-adjacent elements.
53
- */
54
- public static int getMaxSumApproach2 (int [] arr ) {
55
- int n = arr .length ;
56
-
57
- // Two variables to keep track of previous two results:
58
- // prev1 = max sum up to the last element (n-1)
59
- // prev2 = max sum up to the element before last (n-2)
60
-
61
- int prev1 = arr [0 ]; // Base case: Maximum sum for the first element.
62
- int prev2 = 0 ;
63
-
64
- for (int ind = 1 ; ind < n ; ind ++) {
65
- // Case 1: Do not take the current element, keep the last max sum.
66
- int notTake = prev1 ;
67
-
68
- // Case 2: Take the current element and add it to the result from two steps
69
- // back.
70
- int take = arr [ind ];
71
- if (ind > 1 ) {
72
- take += prev2 ;
73
- }
74
-
75
- // Calculate the current maximum sum and update previous values.
76
- int current = Math .max (take , notTake );
77
-
78
- // Shift prev1 and prev2 for the next iteration.
79
- prev2 = prev1 ;
80
- prev1 = current ;
81
- }
82
-
83
- return prev1 ;
84
- }
85
- }
10
+ private MaximumSumOfNonAdjacentElements () {}
11
+
12
+ /**
13
+ * Approach 1: Uses a dynamic programming array to store the maximum sum at
14
+ * each index. Time Complexity: O(n) - where n is the length of the input
15
+ * array. Space Complexity: O(n) - due to the additional dp array.
16
+ * @param arr The input array of integers .
17
+ * @return The maximum sum of non-adjacent elements .
18
+ */
19
+ public static int getMaxSumApproach1 ( int [] arr ) {
20
+ int n = arr . length ;
21
+ int [] dp = new int [ n ] ;
22
+
23
+ // Base case: Maximum sum if only one element is present.
24
+ dp [ 0 ] = arr [ 0 ];
25
+
26
+ for ( int ind = 1 ; ind < n ; ind ++) {
27
+
28
+ // Case 1: Do not take the current element, carry forward the previous max
29
+ // sum.
30
+ int notTake = dp [ind - 1 ];
31
+
32
+ // Case 2: Take the current element, add it to the max sum up to two
33
+ // indices before.
34
+ int take = arr [ind ];
35
+ if (ind > 1 ) {
36
+ take += dp [ind - 2 ];
37
+ }
38
+
39
+ // Store the maximum of both choices in the dp array.
40
+ dp [ind ] = Math .max (take , notTake );
41
+ }
42
+
43
+ return dp [n - 1 ];
44
+ }
45
+
46
+ /**
47
+ * Approach 2: Optimized space complexity approach using two variables instead
48
+ * of an array. Time Complexity: O(n) - where n is the length of the input
49
+ * array. Space Complexity: O(1) - as it only uses constant space for two
50
+ * variables.
51
+ * @param arr The input array of integers.
52
+ * @return The maximum sum of non-adjacent elements.
53
+ */
54
+ public static int getMaxSumApproach2 (int [] arr ) {
55
+ int n = arr .length ;
56
+
57
+ // Two variables to keep track of previous two results:
58
+ // prev1 = max sum up to the last element (n-1)
59
+ // prev2 = max sum up to the element before last (n-2)
60
+
61
+ int prev1 = arr [0 ]; // Base case: Maximum sum for the first element.
62
+ int prev2 = 0 ;
63
+
64
+ for (int ind = 1 ; ind < n ; ind ++) {
65
+ // Case 1: Do not take the current element, keep the last max sum.
66
+ int notTake = prev1 ;
67
+
68
+ // Case 2: Take the current element and add it to the result from two
69
+ // steps back.
70
+ int take = arr [ind ];
71
+ if (ind > 1 ) {
72
+ take += prev2 ;
73
+ }
74
+
75
+ // Calculate the current maximum sum and update previous values.
76
+ int current = Math .max (take , notTake );
77
+
78
+ // Shift prev1 and prev2 for the next iteration.
79
+ prev2 = prev1 ;
80
+ prev1 = current ;
81
+ }
82
+
83
+ return prev1 ;
84
+ }
85
+ }
0 commit comments