Skip to content

Update MaximumSumOfNonAdjacentElements.java #5874

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -58,36 +58,35 @@ public static int getMaxSumApproach1(int[] arr) {
* @return The maximum sum of non-adjacent elements.
*/
public static int getMaxSumApproach2(int[] arr) {
// Check for an empty array, return 0 as no elements to sum
if (arr.length == 0) {
return 0; // Check for empty array
return 0;
}
// If there's only one element, return that element as the result
if (arr.length == 1) {
return arr[0];
}

int n = arr.length;

// Two variables to keep track of previous two results:
// prev1 = max sum up to the last element (n-1)
// prev2 = max sum up to the element before last (n-2)
// Initialize two variables to store the maximum sums:
int prev1 = arr[0]; // Maximum sum up to the first element.
int prev2 = 0; // Base case for the element before the first.

int prev1 = arr[0]; // Base case: Maximum sum for the first element.
int prev2 = 0;
// Iterate over the array starting from the second element.
for (int ind = 1; ind < arr.length; ind++) {
// Calculate the sum if taking the current element:
// Include arr[ind] + prev2 (if index > 1)
int take = arr[ind] + (ind > 1 ? prev2 : 0);

for (int ind = 1; ind < n; ind++) {
// Case 1: Do not take the current element, keep the last max sum.
// Calculate the sum if not taking the current element:
// Take the sum as it is up to the previous element.
int notTake = prev1;

// Case 2: Take the current element and add it to the result from two
// steps back.
int take = arr[ind];
if (ind > 1) {
take += prev2;
}

// Calculate the current maximum sum and update previous values.
// Determine the maximum of taking or not taking the current element.
int current = Math.max(take, notTake);

// Shift prev1 and prev2 for the next iteration.
prev2 = prev1;
prev1 = current;
// Update the values for the next iteration:
prev2 = prev1; // Shift prev2 to the last element's sum.
prev1 = current; // Update prev1 with the current maximum sum.
}

return prev1;
Expand Down