|
17 | 17 | Approach:
|
18 | 18 |
|
19 | 19 | 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 | +*/ |
23 | 22 |
|
| 23 | +// Problem explanation: "https://medium.com/@amitrajit_bose/max-sum-of-non-adjacent-elements-a04ebc4f2602" |
24 | 24 |
|
25 | 25 | public class MaxSumNonAdjacent {
|
| 26 | + |
26 | 27 | // This function recursively calculates the maximum possible sum
|
27 | 28 | // by considering or not considering the current element.
|
28 | 29 | static int solveUtil(int ind, int[] arr, int[] dp) {
|
29 | 30 | // 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 | + |
32 | 35 | // 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 | + |
35 | 40 | // 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 | + |
38 | 45 | // Calculate the maximum sum by either picking the current element or not picking it.
|
39 | 46 | int pick = arr[ind] + solveUtil(ind - 2, arr, dp);
|
40 | 47 | int nonPick = solveUtil(ind - 1, arr, dp);
|
41 |
| - |
| 48 | + |
42 | 49 | // Store the maximum of the two options in the dp array for future reference.
|
43 | 50 | return dp[ind] = Math.max(pick, nonPick);
|
44 | 51 | }
|
45 | 52 |
|
46 | 53 | // This function initializes the dp array and calls the recursive solver.
|
47 | 54 | static int solve(int n, int[] arr) {
|
48 |
| - int dp[] = new int[n]; |
49 |
| - |
| 55 | + int[] dp = new int[n]; |
| 56 | + |
50 | 57 | // Initialize the dp array with -1 to indicate that values are not calculated yet.
|
51 | 58 | Arrays.fill(dp, -1);
|
52 |
| - |
| 59 | + |
53 | 60 | // Call the recursive solver for the last index (n-1).
|
54 | 61 | return solveUtil(n - 1, arr, dp);
|
55 | 62 | }
|
56 | 63 |
|
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: "); |
60 | 69 | int n = sc.nextInt();
|
61 | 70 | 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++) { |
63 | 73 | nums[i] = sc.nextInt();
|
64 | 74 | }
|
| 75 | + |
65 | 76 | // Call the solve function to find the maximum possible sum.
|
66 | 77 | int result = solve(n, nums);
|
67 |
| - |
| 78 | + |
68 | 79 | // Print the result.
|
69 | 80 | System.out.println("Maximum sum of non-adjacent elements: " + result);
|
| 81 | + |
| 82 | + sc.close(); // Close the scanner to prevent resource leakage. |
70 | 83 | }
|
71 | 84 | }
|
72 |
| - |
|
0 commit comments