# Maximum Sum Such That No Two Elements Are Adjacent in Java

In this tutorial, we are going to discuss how to compute the maximum sum such that no two elements are adjacent in Java. In the input, an array (inptArr[]) filled with positive numbers is given.

Example 1:

Input

int inptArr[] = {15, 15, 110, 1100, 110, 15, 7, 80}

Output

1210

Explanation:

We consider the subset {15, 1100, 15, 80}, and we get the sum 1210, which is the maximum sum when the two elements of the subset are not adjacent if we take the reference of the input array.

Example 2:

Input

int inptArr[] = {3, 15, 11, 7, 180}

Output

195

Explanation:

We consider the subset {15, 1100, 15, 80}, and we get the sum 1210, which is the maximum sum when the two elements of the subset are not adjacent if we take the reference of the input array.

## Simple Approach

The simple approach is the brute force approach for this problem. In this approach, we will find all the possible subsets of the given input array. In the found subsets, we will check which one is the valid subset (for this problem, valid subsets are those subsets whose two elements are not adjacent). For the valid subsets, we will be computing the sum and updating our answer as per the largest sum found.

Output:

```For the input array:
15 15 110 1100 110 15 7 80
The Maximum Sum Such That No Two Elements Are Adjacent is: 1210

For the input array:
3 15 11 7 180
The Maximum Sum Such That No Two Elements Are Adjacent is: 195
```

Complexity Analysis: Each recursive call gives birth to a recursive call (one excluding the element, and the other includes the element). Hence, the time complexity of the program is exponential. The space complexity of the program is linear as the recursive calls are using stack. Thus, the time complexity of the program is O(2n). The space complexity of the program O(n), where n is the total size of the input array.

## Approach: Using Dynamic Programming

The above program shows that each element has two choices. If that element is selected, then its neighbors can not be selected. If that element is not selected, then its neighbors can or can not be selected.

Hence, the maximum sum till the jth index has two possibilities: one is the subsequence that includes inputArr[j] or the other one that excludes inputArr[j].

If we include the element inputArr[j] is included, then the maximum sum is dependent on the maximum sum of subsequence until (j - 1)th element. It is obvious that we will be excluding element inputArr[j - 1], as inputArr[j] is already included.

If we exclude the element inputArr[j], then the maximum sum is the same as the maximum sum of subsequences till (j - 1), where the element inputArr[j - 1] can either be included or excluded.

So we have to build a 2-D array dpArr[N] where dp[i] keeps the maximum sum of subsequence till jth index with inputArr[i] excluded, and dpArr[j] keeps the sum when the inputArr[i] is included.

Thus, we get the following relation in this dynamic programming approach.

dpArr[j] = dpArr[j - 1] + inputArr[j] and dpArr[j] = max(dpArr[j - 1], dpArr[j - 1]).

Now, see the following steps required to achieve the solution.

Step 1: If the array size is 1, then the answer is inputArr.

Step 2: Initialize the values of dpArr = 0 and dpArr = inputArr.

Step 3: Iterate from j = 1 to Siz - 1:

• Fill the dpArr[] array according to the above-shown relation.

Step 3: Return the maximum between dpArr[Siz - 1] and dpArr[Siz - 1].

Observe the following program.

Output:

```For the input array:
15 15 110 1100 110 15 7 80
The Maximum Sum Such That No Two Elements Are Adjacent is: 1210

For the input array:
3 15 11 7 180
The Maximum Sum Such That No Two Elements Are Adjacent is: 195
```

Complexity Analysis: There is only one loop that is required to find the maximum sum. Hence, the time complexity of the program is O(n). Also, the program uses an array that contains twice the number of input elements. Hence, the space complexity of the program is O(2 x n), asymptotically represented as O(n), where n is the total number of elements present in the input array.

## Space Optimized Approach

If we look at the above program, we see that the current state of the jth element is only dependent on the two states of the last element encountered. Therefore, instead of the created an array, we can only use two variables.

One variable is inclsv, which keeps the value of the maximum sum of subsequence till j - 1 when inputArr[j - 1] is included.

Another variable is exclsv, which keeps the value of the maximum sum of subsequence till j - 1 when inputArr[j - 1] is excluded.

### Illustration:

Output:

```For the input array:
15 15 110 1100 110 15 7 80
The Maximum Sum Such That No Two Elements Are Adjacent is: 1210

For the input array:
3 15 11 7 180
The Maximum Sum Such That No Two Elements Are Adjacent is: 195
```

Complexity Analysis: The time complexity of the program is the same as the last program, whereas the space complexity of the program is O(1).

### Feedback   