# Maximum sum in circular array such that no two elements are adjacent | Set 2

Given an **array arr[]** of positive numbers, find the maximum sum of a subsequence with the constraint that no 2 numbers in the sequence should be adjacent in the array where the last and the first elements are assumed adjacent.

**Examples:**

Input:arr[] = {3, 5, 3}

Output:5

Explanation:

We cannot take the first and last elements because they are considered to be adjacent, hence the output is 5.

Inputarr[] = {1, 223, 41, 4, 414, 5, 16}

Output:653

Explanation:

Taking elements form the index 1, 4 and 6 we get the maximum sum as 653.

**Approach:** The idea is to use Memorization algorithm to solve the problem mentioned above. The most important observation is that the **first and the last elements can never be chosen together**. So, we can break the problem into two parts:

- the maximum sum we can get from index
**0 to size of array – 2** - the maximum sum we can get from index
**1 to size of array – 1**

The answer wil be the maximum of these two sums which can be solved by using **Dynamic Programming**.

**Below is the implementation of the above approach:**

`// C++ program to find maximum sum ` `// in circular array such that ` `// no two elements are adjacent ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Store the maximum possible at each index ` `vector<` `int` `> dp; ` ` ` `int` `maxSum(` `int` `i, vector<` `int` `>& subarr) ` `{ ` ` ` ` ` `// When i exceeds the index of the ` ` ` `// last element simply return 0 ` ` ` `if` `(i >= subarr.size()) ` ` ` `return` `0; ` ` ` ` ` `// If the value has already been calculated, ` ` ` `// directly return it from the dp array ` ` ` `if` `(dp[i] != -1) ` ` ` `return` `dp[i]; ` ` ` ` ` `// The next states are don't take ` ` ` `// this element and go to (i + 1)th state ` ` ` `// else take this element ` ` ` `// and go to (i + 2)th state ` ` ` `return` `dp[i] ` ` ` `= max(maxSum(i + 1, subarr), ` ` ` `subarr[i] ` ` ` `+ maxSum(i + 2, subarr)); ` `} ` ` ` `// function to find the max value ` `int` `Func(vector<` `int` `> arr) ` `{ ` ` ` `vector<` `int` `> subarr = arr; ` ` ` ` ` `// subarr contains elements ` ` ` `// from 0 to arr.size() - 2 ` ` ` `subarr.pop_back(); ` ` ` ` ` `// Initializing all the values with -1 ` ` ` `dp.resize(subarr.size(), -1); ` ` ` ` ` `// Calculating maximum possible ` ` ` `// sum for first case ` ` ` `int` `max1 = maxSum(0, subarr); ` ` ` ` ` `subarr = arr; ` ` ` ` ` `// subarr contains elements ` ` ` `// from 1 to arr.size() - 1 ` ` ` `subarr.erase(subarr.begin()); ` ` ` ` ` `dp.clear(); ` ` ` ` ` `// Re-initializing all values with -1 ` ` ` `dp.resize(subarr.size(), -1); ` ` ` ` ` `// Calculating maximum possible ` ` ` `// sum for second case ` ` ` `int` `max2 = maxSum(0, subarr); ` ` ` ` ` `// Printing the maximum between them ` ` ` `cout << max(max1, max2) << endl; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` ` ` `vector<` `int` `> arr = { 1, 2, 3, 1 }; ` ` ` ` ` `Func(arr); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

4

**Time Complexity:** O(N)

**Auxiliary Space Complexity:** O(N)

**Similar article:** Maximum sum in circular array such that no two elements are adjacent

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: **DSA Self Paced**. Become industry ready at a student-friendly price.

## Recommended Posts:

- Maximum sum in circular array such that no two elements are adjacent
- Minimize the maximum absolute difference of adjacent elements in a circular array
- Minimum absolute difference of adjacent elements in a circular array
- Maximum set bit sum in array without considering adjacent elements
- Arrange N elements in circular fashion such that all elements are strictly less than sum of adjacent elements
- Minimize the maximum difference between adjacent elements in an array
- Maximum sum of Array formed by replacing each element with sum of adjacent elements
- Missing occurrences of a number in an array such that maximum absolute difference of adjacent elements is minimum
- Maximum sum such that no two elements are adjacent
- Maximum sum such that no two elements are adjacent | Set 2
- Maximum sum in a 2 x n grid such that no two elements are adjacent
- Maximum subsequence sum of at most K-distant adjacent elements
- Maximum sum such that exactly half of the elements are selected and no two adjacent
- Sort elements of an array in increasing order of absolute difference of adjacent elements
- Maximum length subsequence with difference between adjacent elements as either 0 or 1
- Find maximum sum from top to bottom row with no adjacent diagonal elements
- Minimize the maximum difference of adjacent elements after at most K insertions
- Maximum length subarray with difference between adjacent elements as either 0 or 1
- Maximum sub-sequence sum such that indices of any two adjacent elements differs at least by 3
- Count of array elements which is smaller than both its adjacent elements

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.