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.

Input arr[] = {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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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


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.




My Personal Notes arrow_drop_up

Recommended Posts:


Check out this Author's contributed articles.

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.