Skip to content

Commit fedf596

Browse files
committed
Add Maximum Sum of Non-Adjacent Elements Algorithm
1 parent 7bcf2dc commit fedf596

File tree

1 file changed

+31
-19
lines changed

1 file changed

+31
-19
lines changed

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

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,56 +17,68 @@
1717
Approach:
1818
1919
Use dynamic programming to maintain a running maximum sum.
20-
For each element, decide to either include it in the sum (and skip the previous element) or exclude it (and keep the sum up to the previous element).*/
21-
22-
// Problem Explaination: "https://medium.com/@amitrajit_bose/max-sum-of-non-adjacent-elements-a04ebc4f2602"
20+
For each element, decide to either include it in the sum (and skip the previous element) or exclude it (and keep the sum up to the previous element).
21+
*/
2322

23+
// Problem explanation: "https://medium.com/@amitrajit_bose/max-sum-of-non-adjacent-elements-a04ebc4f2602"
2424

2525
public class MaxSumNonAdjacent {
26+
2627
// This function recursively calculates the maximum possible sum
2728
// by considering or not considering the current element.
2829
static int solveUtil(int ind, int[] arr, int[] dp) {
2930
// If the index is negative, there are no elements left to consider.
30-
if (ind < 0) return 0;
31-
31+
if (ind < 0) {
32+
return 0;
33+
}
34+
3235
// If the index is 0, there is only one element to consider, so return its value.
33-
if (ind == 0) return arr[ind];
34-
36+
if (ind == 0) {
37+
return arr[ind];
38+
}
39+
3540
// If we have already calculated the result for this index, return it.
36-
if (dp[ind] != -1) return dp[ind];
37-
41+
if (dp[ind] != -1) {
42+
return dp[ind];
43+
}
44+
3845
// Calculate the maximum sum by either picking the current element or not picking it.
3946
int pick = arr[ind] + solveUtil(ind - 2, arr, dp);
4047
int nonPick = solveUtil(ind - 1, arr, dp);
41-
48+
4249
// Store the maximum of the two options in the dp array for future reference.
4350
return dp[ind] = Math.max(pick, nonPick);
4451
}
4552

4653
// This function initializes the dp array and calls the recursive solver.
4754
static int solve(int n, int[] arr) {
48-
int dp[] = new int[n];
49-
55+
int[] dp = new int[n];
56+
5057
// Initialize the dp array with -1 to indicate that values are not calculated yet.
5158
Arrays.fill(dp, -1);
52-
59+
5360
// Call the recursive solver for the last index (n-1).
5461
return solveUtil(n - 1, arr, dp);
5562
}
5663

57-
public static void main(String args[]) {
58-
Scanner sc= new Scanner(System.in);
59-
// Input array size with elements.
64+
public static void main(String[] args) {
65+
Scanner sc = new Scanner(System.in);
66+
67+
// Input array size and elements.
68+
System.out.print("Enter the size of the array: ");
6069
int n = sc.nextInt();
6170
int[] nums = new int[n];
62-
for(int i=0;i<n;i++){
71+
System.out.print("Enter the elements of the array: ");
72+
for (int i = 0; i < n; i++) {
6373
nums[i] = sc.nextInt();
6474
}
75+
6576
// Call the solve function to find the maximum possible sum.
6677
int result = solve(n, nums);
67-
78+
6879
// Print the result.
6980
System.out.println("Maximum sum of non-adjacent elements: " + result);
81+
82+
sc.close(); // Close the scanner to prevent resource leakage.
7083
}
7184
}
72-

0 commit comments

Comments
 (0)