Maximum sum in circular array such that no two elements are adjacent

16 Jun, 2022
Given a circular array containing of positive integers value. The task is to find the maximum sum of a subsequence with the constraint that no 2 numbers in the sequence should be adjacent in the array.Examples:  
Input: circular arr = {1, 2, 3, 1}
Output : 4
subsequence will be(1, 3), hence 1 + 3 = 4 

Input: circular arr = {1, 2, 3, 4, 5, 1}
Output: 9
subsequence will be(1, 3, 5), hence 1 + 3 + 5 = 9 
 
Naive Approach: This problem is extension to the problem https://www.geeksforgeeks.org/find-maximum-possible-stolen-value-houses/ .In this problem the arr elements are arranged in circular fashion so the 1st and the last element are also adjacent. So we can not take first and last element together so we can take either of the element1 or last element. So first we can take element 1 and exclude last element  and calculate the maximum sum subsequence where no two selected elements are adjacent and then also we can calculate another answer by excluding 1st element and including last element. And we will take maximum of two.
Implementation of recursion approach:
C++
// CPP program to find maximum sum in a circular array// such that no elements are adjacent in the sum.#include<iostream>using namespace std; // calculate the maximum stolen valueint helper(int* arr, int n){    // base case    if (n < 0) {        return 0;    }     if (n == 0) {        return arr[0];    }     // if current element is pick then previous cannot be    // picked    int pick = arr[n] + helper(arr, n - 2);    // if current element is not picked then previous    // element is picked    int notPick = helper(arr, n - 1);     // return max of picked and not picked    return max(pick, notPick);} int maxLoot(int* arr, int n){    // case 1: Last elenent is exluded we have to calculate    // result for first n-1 houses.    int ans1 = helper(arr, n - 1);    // case 2: First element is excluded we have to    // calculate result 2 to n houses.    int ans2 = helper(arr + 1, n - 1);    return max(ans1, ans2);}// Driver to test above codeint main(){    int arr[] = { 1, 2, 3, 1 };    int n = sizeof(arr) / sizeof(arr[0]);    cout << "Maximum loot possible : "         << maxLoot(arr, n - 1);    return 0;}// This code is contributed by Sanskar Bharadia
Maximum loot possible : 4
Complexity Analysis:
Time Complexity:  O(2n).
Auxiliary Space: O(N)
Approach The problem can be solved using DP. An approach has already been discussed in this post, but it for an array. We can treat the circular subarray a two arrays one from (0th to n-2-th) and (1st to n-1-th) index, and use the approach used in the previous post. The maximum sum returned by both will be the answer.  
 
Below is the implementation of the above approach.  
C++
Java
Python 3
C#
PHP
Javascript
// CPP program to find maximum sum in a circular array// such that no elements are adjacent in the sum.#include <bits/stdc++.h>using namespace std; // Function to calculate the sum// from 0th position to(n-2)th positionint maxSum1(int arr[], int n){    int dp[n];    int maxi = 0;     for (int i = 0; i < n - 1; i++) {         // copy the element of original array to dp[]        dp[i] = arr[i];         // find the maximum element in the array        if (maxi < arr[i])            maxi = arr[i];    }     // start from 2nd to n-1th pos    for (int i = 2; i < n - 1; i++) {         // traverse for all pairs        // bottom-up approach        for (int j = 0; j < i - 1; j++) {             // dp-condition            if (dp[i] < dp[j] + arr[i]) {                dp[i] = dp[j] + arr[i];                 // find maximum sum                if (maxi < dp[i])                    maxi = dp[i];            }        }    }     // return the maximum    return maxi;} // Function to find the maximum sum// from 1st position to n-1-th positionint maxSum2(int arr[], int n){    int dp[n];    int maxi = 0;     for (int i = 1; i < n; i++) {        dp[i] = arr[i];         if (maxi < arr[i])            maxi = arr[i];    }     // Traverse from third to n-th pos    for (int i = 3; i < n; i++) {         // bottom-up approach        for (int j = 1; j < i - 1; j++) {             // dp condition            if (dp[i] < arr[i] + dp[j]) {                dp[i] = arr[i] + dp[j];                 // find max sum                if (maxi < dp[i])                    maxi = dp[i];            }        }    }     // return max    return maxi;} int findMaxSum(int arr[], int n){   return max(maxSum1(arr, n), maxSum2(arr, n));} // Driver Codeint main(){    int arr[] = { 1, 2, 3, 1 };    int n = sizeof(arr)/sizeof(arr[0]);    cout << findMaxSum(arr, n);    return 0;}
// Java  program to find maximum sum in a circular array// such that no elements are adjacent in the sum. import java.io.*; class GFG {          // Function to calculate the sum// from 0th position to(n-2)th positionstatic int maxSum1(int arr[], int n){    int dp[]=new int[n];    int maxi = 0;     for (int i = 0; i < n - 1; i++) {         // copy the element of original array to dp[]        dp[i] = arr[i];         // find the maximum element in the array        if (maxi < arr[i])            maxi = arr[i];    }     // start from 2nd to n-1th pos    for (int i = 2; i < n - 1; i++) {         // traverse for all pairs        // bottom-up approach        for (int j = 0; j < i - 1; j++) {             // dp-condition            if (dp[i] < dp[j] + arr[i]) {                dp[i] = dp[j] + arr[i];                 // find maximum sum                if (maxi < dp[i])                    maxi = dp[i];            }        }    }     // return the maximum    return maxi;} // Function to find the maximum sum// from 1st position to n-1-th positionstatic int maxSum2(int arr[], int n){    int dp[]=new int[n];    int maxi = 0;     for (int i = 1; i < n; i++) {        dp[i] = arr[i];         if (maxi < arr[i])            maxi = arr[i];    }     // Traverse from third to n-th pos    for (int i = 3; i < n; i++) {         // bottom-up approach        for (int j = 1; j < i - 1; j++) {             // dp condition            if (dp[i] < arr[i] + dp[j]) {                dp[i] = arr[i] + dp[j];                 // find max sum                if (maxi < dp[i])                    maxi = dp[i];            }        }    }     // return max    return maxi;} static int findMaxSum(int arr[], int n){    int t=Math.max(maxSum1(arr, n), maxSum2(arr, n));    return  t;} // Driver Code    public static void main (String[] args) {                 int arr[] = { 1, 2, 3, 1 };        int n = arr.length;        System.out.println(findMaxSum(arr, n));             }}
# Python 3 program to find maximum sum# in a circular array such that no# elements are adjacent in the sum. # Function to calculate the sum from# 0th position to(n-2)th positiondef maxSum1(arr, n):     dp = [0] * n    maxi = 0     for i in range(n - 1):         # copy the element of original        # array to dp[]        dp[i] = arr[i]         # find the maximum element in the array        if (maxi < arr[i]):            maxi = arr[i]     # start from 2nd to n-1th pos    for i in range(2, n - 1):         # traverse for all pairs bottom-up        # approach        for j in range(i - 1) :             # dp-condition            if (dp[i] < dp[j] + arr[i]):                dp[i] = dp[j] + arr[i]                 # find maximum sum                if (maxi < dp[i]):                    maxi = dp[i]     # return the maximum    return maxi # Function to find the maximum sum# from 1st position to n-1-th positiondef maxSum2(arr, n):     dp = [0] * n    maxi = 0     for i in range(1, n):        dp[i] = arr[i]         if (maxi < arr[i]):            maxi = arr[i]     # Traverse from third to n-th pos    for i in range(3, n):         # bottom-up approach        for j in range(1, i - 1) :             # dp condition            if (dp[i] < arr[i] + dp[j]):                dp[i] = arr[i] + dp[j]                 # find max sum                if (maxi < dp[i]):                    maxi = dp[i]     # return max    return maxi def findMaxSum(arr, n):    return max(maxSum1(arr, n), maxSum2(arr, n)) # Driver Codeif __name__ == "__main__":         arr = [ 1, 2, 3, 1 ]    n = len(arr)    print(findMaxSum(arr, n)) # This code is contributed by ita_c
// C# program to find maximum sum// in a circular array such that// no elements are adjacent in the sum.using System; class GFG{// Function to calculate the sum// from 0th position to(n-2)th positionstatic int maxSum1(int []arr, int n){    int []dp = new int[n];    int maxi = 0;     for (int i = 0; i < n - 1; i++)    {         // copy the element of original        // array to dp[]        dp[i] = arr[i];         // find the maximum element        // in the array        if (maxi < arr[i])            maxi = arr[i];    }     // start from 2nd to n-1th pos    for (int i = 2; i < n - 1; i++)    {         // traverse for all pairs        // bottom-up approach        for (int j = 0; j < i - 1; j++)        {             // dp-condition            if (dp[i] < dp[j] + arr[i])            {                dp[i] = dp[j] + arr[i];                 // find maximum sum                if (maxi < dp[i])                    maxi = dp[i];            }        }    }     // return the maximum    return maxi;} // Function to find the maximum sum// from 1st position to n-1-th positionstatic int maxSum2(int []arr, int n){    int []dp = new int[n];    int maxi = 0;     for (int i = 1; i < n; i++)    {        dp[i] = arr[i];         if (maxi < arr[i])            maxi = arr[i];    }     // Traverse from third to n-th pos    for (int i = 3; i < n; i++)    {         // bottom-up approach        for (int j = 1; j < i - 1; j++)        {             // dp condition            if (dp[i] < arr[i] + dp[j])            {                dp[i] = arr[i] + dp[j];                 // find max sum                if (maxi < dp[i])                    maxi = dp[i];            }        }    }     // return max    return maxi;} static int findMaxSum(int []arr, int n){    int t = Math.Max(maxSum1(arr, n),                     maxSum2(arr, n));    return t;} // Driver Codestatic public void Main (){    int []arr = { 1, 2, 3, 1 };    int n = arr.Length;    Console.WriteLine(findMaxSum(arr, n));}} // This code is contributed// by Sach_Code
<?php// PHP program to find maximum sum in// a circular array such that no// elements are adjacent in the sum.// Function to calculate the sum// from 0th position to(n-2)th position function maxSum1($arr, $n){    $dp[$n] = array();    $maxi = 0;     for ($i = 0; $i < $n - 1; $i++)    {         // copy the element of original        // array to dp[]        $dp[$i] = $arr[$i];         // find the maximum element in the array        if ($maxi < $arr[$i])            $maxi = $arr[$i];    }     // start from 2nd to n-1th pos    for ($i = 2; $i < $n - 1; $i++)    {         // traverse for all pairs        // bottom-up approach        for ( $j = 0; $j < $i - 1; $j++)        {             // dp-condition            if ($dp[$i] < $dp[$j] + $arr[$i])            {                $dp[$i] = $dp[$j] + $arr[$i];                 // find maximum sum                if ($maxi < $dp[$i])                    $maxi = $dp[$i];            }        }    }     // return the maximum    return $maxi;} // Function to find the maximum sum// from 1st position to n-1-th positionfunction maxSum2($arr, $n){    $dp[$n] = array();    $maxi = 0;     for ($i = 1; $i < $n; $i++)    {        $dp[$i] = $arr[$i];         if ($maxi < $arr[$i])            $maxi = $arr[$i];    }     // Traverse from third to n-th pos    for ($i = 3; $i < $n; $i++)    {         // bottom-up approach        for ($j = 1; $j < $i - 1; $j++)        {             // dp condition            if ($dp[$i] < $arr[$i] + $dp[$j])            {                $dp[$i] = $arr[$i] + $dp[$j];                 // find max sum                if ($maxi < $dp[$i])                    $maxi = $dp[$i];            }        }    }     // return max    return $maxi;} function findMaxSum($arr, $n){    return max(maxSum1($arr, $n),               maxSum2($arr, $n));} // Driver Code$arr = array(1, 2, 3, 1 );$n = sizeof($arr);echo findMaxSum($arr, $n);     //  This code is contributed// by Sach_Code?>
<script> // JavaScript program to find maximum sum// in a circular array such that// no elements are adjacent in the sum. // Function to calculate the sum// from 0th position to(n-2)th positionfunction maxSum1(arr, n){    let dp = new Array(n);    let maxi = 0;     for (i = 0; i < n - 1; i++)    {         // copy the element of original        // array to dp[]        dp[i] = arr[i];         // find the maximum element        // in the array        if (maxi < arr[i])            maxi = arr[i];    }     // start from 2nd to n-1th pos    for (i = 2; i < n - 1; i++)    {         // traverse for all pairs        // bottom-up approach        for (j = 0; j < i - 1; j++)        {             // dp-condition            if (dp[i] < dp[j] + arr[i])            {                dp[i] = dp[j] + arr[i];                 // find maximum sum                if (maxi < dp[i])                    maxi = dp[i];            }        }    }     // return the maximum    return maxi;} // Function to find the maximum sum// from 1st position to n-1-th positionfunction maxSum2(arr, n){    let dp = new Array(n);    let maxi = 0;     for (i = 1; i < n; i++)    {        dp[i] = arr[i];         if (maxi < arr[i])            maxi = arr[i];    }     // Traverse from third to n-th pos    for (i = 3; i < n; i++)    {         // bottom-up approach        for (j = 1; j < i - 1; j++)        {             // dp condition            if (dp[i] < arr[i] + dp[j])            {                dp[i] = arr[i] + dp[j];                 // find max sum                if (maxi < dp[i])                    maxi = dp[i];            }        }    }     // return max    return maxi;} function findMaxSum(arr, n){    let t = Math.max(maxSum1(arr, n),                     maxSum2(arr, n));    return t;} // Driver Code let arr = [1, 2, 3, 1 ];let n = arr.length;document.write(findMaxSum(arr, n)); // This code is contributed by mohit kumar 29.</script>
4
Time Complexity: O(N^2) 
Space optimization approach: As we can see the dp[i] is calculated form previous values so we can just store those previous values in variables.
C++
// C++ program to find the maximum stolen value#include <iostream>using namespace std; // calculate the maximum stolen valueint findMaxSum(int arr[], int n){    if (n == 0)        return 0;     int value1 = arr[0];    if (n == 1)        return value1;     int value2 = 0;     // case 1: check for 1 to n-1 houses.    // contains maximum stolen value at the end    int max_val1, max_val2;     // Fill remaining positions    for (int i = 1; i < n - 1; i++) {        int first = value1;        int second = arr[i];        if (i > 1) {            second += value2;        }        int curr = max(first, second);         value2 = value1;        value1 = curr;    }    //  case 2: check for 2 to n houses.    max_val1 = value1;    value1 = arr[1];    value2 = 0;    for (int i = 2; i < n; i++) {        int first = value1;        int second = arr[i];        if (i > 1) {            second += value2;        }        int curr = max(first, second);         value2 = value1;        value1 = curr;    }    max_val2 = value1;    return max(max_val1, max_val2);} // Driver to test above codeint main(){    // Value of houses    int arr[] = { 6, 7, 1, 3, 8, 2, 4 };    int n = sizeof(arr) / sizeof(arr[0]);    cout << findMaxSum(arr, n);    return 0;}
19
Complexity Analysis:
Time Complexity:  O(N).
Space Complexity: O(1). 
Mohd_Saliem
Sach_Code
ukasp
mohit kumar 29
kapoorsagar226
sanskar84
Arrays
Dynamic Programming
Arrays
Dynamic Programming
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.