Related Articles

Related Articles

Maximum sum in circular array such that no two elements are adjacent | Set 2
  • Last Updated : 23 Sep, 2020

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:
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 the 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 the size of the array – 2
  • The maximum sum we can get from index 1 to size of the array – 1

The answer will be the maximum of these two sums which can be solved by using Dynamic Programming
Below is the implementation of the above approach: 

C++

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


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find maximum sum
// in circular array such that
// no two elements are adjacent
import java.util.*;
class GFG{
 
// Store the maximum
// possible at each index
static Vector<Integer> dp =
              new Vector<>();
 
static int maxSum(int i,
                  Vector<Integer> 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.get(i) != -1)
    return dp.get(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
  dp.add(i, Math.max(maxSum(i + 1, subarr),
                     subarr.get(i) +
                     maxSum(i + 2, subarr)));
  return dp.get(i);
}
 
// function to find the max value
static void Func(Vector<Integer> arr)
{
  Vector<Integer> subarr =
         new Vector<>();
  subarr.addAll(arr);
 
  // subarr contains elements
  // from 0 to arr.size() - 2
  subarr.remove(subarr.size() - 1);
 
  // Initializing all the values with -1
  dp.setSize(subarr.size());
  Collections.fill(dp, -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.remove(0);
 
  dp.clear();
 
  // Re-initializing all values with -1
  dp.setSize(subarr.size());
  Collections.fill(dp, -1);
 
 
  // Calculating maximum possible
  // sum for second case
  int max2 = maxSum(0, subarr);
 
  // Printing the maximum between them
  System.out.print(Math.max(max1, max2) + "\n");
}
 
// Driver code
public static void main(String[] args)
{
  Vector<Integer> arr =new Vector<>();
  arr.add(1);
  arr.add(2);
  arr.add(3);
  arr.add(1);
  Func(arr);
}
 
// This code is contributed by gauravrajput1

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to find maximum sum
# in circular array such that
# no two elements are adjacent
 
# Store the maximum possible at each index
dp = []
 
def maxSum(i, subarr):
 
    # When i exceeds the index of the
    # last element simply return 0
    if (i >= len(subarr)):
        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
    dp[i] = max(maxSum(i + 1, subarr),
                subarr[i] +
                maxSum(i + 2, subarr))
    return dp[i]
 
# function to find the max value
def Func(arr):
    subarr = arr
 
    # subarr contains elements
    # from 0 to arr.size() - 2
    subarr.pop()
    global dp
     
    # Initializing all the values with -1
    dp= [-1] * (len(subarr))
 
    # Calculating maximum possible
    # sum for first case
    max1 = maxSum(0, subarr)
 
    subarr = arr
 
    # subarr contains elements
    # from 1 to arr.size() - 1
    subarr = subarr[:]
 
    del dp
 
    # Re-initializing all values with -1
    dp = [-1] * (len(subarr))
 
    # Calculating maximum possible
    # sum for second case
    max2 = maxSum(0, subarr)
 
    # Printing the maximum between them
    print(max(max1, max2))
 
# Driver code
if __name__ == "__main__":
    arr = [1, 2, 3, 1]
    Func(arr)
     
# This code is contributed by Chitranayal

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
 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up
Recommended Articles
Page :