Skip to content

Commit 8690f80

Browse files
authored
Merge pull request #89 from jaebradley/maximum-sum-non-adjacent-elements
implement maximum non adjacent element sum
2 parents 299e8d5 + 7f0c985 commit 8690f80

File tree

3 files changed

+155
-0
lines changed

3 files changed

+155
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
## Purpose
2+
3+
I recently came across an interview question that stumped me for a while
4+
> Given a list of integers, write a function that returns the largest sum of non-adjacent numbers.
5+
6+
I am looking for feedback around both my implementation and the correctness of my proposed solution (I have some test
7+
cases, but I could have missed a case).
8+
9+
## Approach
10+
11+
For arrays with `1` or `2` values, the answer is trivial.
12+
13+
For arrays with more than `2` values, keep track of the maximum sum from the preceding non-adjacent elements that
14+
*includes the most recent non-adjacent element*, and the maximum sum from the preceding non-adjacent elements that
15+
*excludes the most recent non-adjacent element*.
16+
17+
So for `[3, 1, 1, 5, 1]` at index `3`, the maximum sum from preceding non-adjacent elements that includes the most recent
18+
non-adjacent element is `1`, while the maximum sum from preceding non-adjacent elements that excludes the most recent
19+
non-adjacent element is `3`.
20+
21+
Taking the max of these two sums and adding them to the current value will produce the maximum sum for non-adjacent
22+
elements up to that index. Continuing this process for each remaining index and then comparing the last two calculated
23+
sums will produce the maximum sum.
24+
25+
Let's walk through this process with `[3, 1, 1, 5, 1]`.
26+
27+
* At index `0`, the max sum is `3`
28+
* At index `1`, the max sum is `1`
29+
* At index `2`, the max sum from including the most recent non-adjacent element is `3`, while the max sum from excluding
30+
the most recent non-adjacent element is `0`. Thus, we choose `3` and add that to the current value (`1`). `4` is now
31+
the max non-adjacent sum for index `2`.
32+
* At index `3`, the max sum from including the most recent non-adjacent element is `1`, while the max sum from excluding
33+
the most recent non-adjacent element is `3`. Thus, we choose `3` and add that to the current value (`5`). `8` is now
34+
the max non-adjacent sum for index `3`.
35+
* At index `4`, the max sum from including the most recent non-adjacent element is `4`, while the max sum from excluding
36+
the most recent non-adjacent element is `3`. Thus, we choose `4` and add that to the current value (`1`). `6` is now
37+
the max non-adjacent sum for index `4`.
38+
* The final two sums were `8` and `6` - we take the max (`8`).
39+
40+
Apologies if this was confusing to follow, it was difficult to describe my thought process.
41+
42+
## Implementation
43+
44+
<!-- language: lang-java -->
45+
46+
public class MaximumNonAdjacentElementSumIdentifier {
47+
public static int identify(int[] values) {
48+
if (values == null || values.length == 0) {
49+
throw new RuntimeException("Unable to identify sum");
50+
}
51+
52+
if (values.length == 1) {
53+
return values[0];
54+
}
55+
56+
int firstSum = 0;
57+
int secondSum = values[0];
58+
int previousValueSum = values[1];
59+
60+
for (int i = 2; i < values.length; i++) {
61+
int value = values[i];
62+
int maximumCurrentElementSum = Math.max(firstSum, secondSum) + value;
63+
firstSum = Math.max(firstSum, secondSum);
64+
secondSum = previousValueSum;
65+
previousValueSum = maximumCurrentElementSum;
66+
}
67+
68+
return Math.max(secondSum, previousValueSum);
69+
}
70+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package problems.impl;
2+
3+
/**
4+
* Given a list of integers, write a function that returns the largest sum of non-adjacent numbers.
5+
*/
6+
7+
public class MaximumNonAdjacentElementSumIdentifier {
8+
public static int identify(int[] values) {
9+
if (values == null || values.length == 0) {
10+
throw new RuntimeException("Unable to identify sum");
11+
}
12+
13+
if (values.length == 1) {
14+
return values[0];
15+
}
16+
17+
int firstSum = 0;
18+
int secondSum = values[0];
19+
int previousValueSum = values[1];
20+
21+
for (int i = 2; i < values.length; i++) {
22+
int value = values[i];
23+
int maximumCurrentElementSum = Math.max(firstSum, secondSum) + value;
24+
firstSum = Math.max(firstSum, secondSum);
25+
secondSum = previousValueSum;
26+
previousValueSum = maximumCurrentElementSum;
27+
}
28+
29+
return Math.max(secondSum, previousValueSum);
30+
}
31+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package problems.impl;
2+
3+
import org.junit.Test;
4+
5+
import static org.junit.Assert.*;
6+
7+
public class MaximumNonAdjacentElementSumIdentifierTest {
8+
@Test
9+
public void itShouldIdentifySum1() {
10+
int[] values = new int[] { 5, 1, 1, 7, 3};
11+
int expected = 12;
12+
assertEquals(expected, MaximumNonAdjacentElementSumIdentifier.identify(values));
13+
}
14+
15+
@Test
16+
public void itShouldThrowAnExceptionForNullValues() {
17+
try {
18+
MaximumNonAdjacentElementSumIdentifier.identify(null);
19+
} catch (RuntimeException e) {
20+
// expected
21+
}
22+
}
23+
24+
@Test
25+
public void itShouldThrowAnExceptionForEmptyValues() {
26+
try {
27+
MaximumNonAdjacentElementSumIdentifier.identify(new int[] {});
28+
} catch (RuntimeException e) {
29+
// expected
30+
}
31+
}
32+
33+
@Test
34+
public void itShouldReturnValueForSingleElementValues() {
35+
int value = 5;
36+
int[] values = new int[] { value };
37+
assertEquals(value, MaximumNonAdjacentElementSumIdentifier.identify(values));
38+
}
39+
40+
@Test
41+
public void itShouldReturnGreaterValueForTwoElementValues() {
42+
assertEquals(5, MaximumNonAdjacentElementSumIdentifier.identify(new int[]{4, 5}));
43+
}
44+
45+
@Test
46+
public void itShouldReturnGreatestSingleValueForThreeElementValues() {
47+
assertEquals(3, MaximumNonAdjacentElementSumIdentifier.identify(new int[]{-1, 1, 3}));
48+
}
49+
50+
@Test
51+
public void itShouldReturnGreatestCombinedValuesForThreeElementValues() {
52+
assertEquals(8, MaximumNonAdjacentElementSumIdentifier.identify(new int[]{2, 7, 6}));
53+
}
54+
}

0 commit comments

Comments
 (0)