Open In App

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

Improve
Improve
Like Article
Like
Save
Share
Report

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 value
int 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 element is excluded 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 code
int 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


Java




// Java program to find maximum sum in a circular array
// such that no elements are adjacent in the sum.
  
import java.io.*;
  
class GFG {
 
// calculate the maximum stolen value    
static int helper(int arr[],int l, 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,l, n - 2);
    // if current element is not picked then previous
    // element is picked
    int notPick = helper(arr,l, n - 1);
  
    // return max of picked and not picked
    return Math.max(pick, notPick);
}
static int findMaxSum(int arr[], int n)
{
    // case 1: Last element is excluded we have to calculate
    // result for first n-1 houses.
    int ans1 = helper(arr,0, 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 Math.max(ans1, ans2);
}
// Driver Code
    public static void main (String[] args) {
          
        int arr[] = { 1, 2, 3, 1 };
        int n = arr.length;
        System.out.println("Maximum loot possible : " + findMaxSum(arr, n));
    }
}
//this article is contributed by aditya942003patil


Python3




# Python3 program to find maximum sum in a circular array
# such that no elements are adjacent in the sum.
 
# calculate the maximum stolen value
def helper(arr, n):
    # base case
    if n < 0:
        return 0
    if n == 0:
        return arr[0]
       
    # if current element is pick then previous cannot be
    # picked
    pick = arr[n] + helper(arr, n - 2)
     
    # if current element is not picked then previous
    # element is picked
    notPick = helper(arr, n - 1)
 
    # return max of picked and not picked
    return max(pick, notPick)
 
def maxLoot(arr, n):
    # case 1: Last element is excluded we have to calculate
    # result for first n-1 houses.
    ans1 = helper(arr, n - 1)
     
    # case 2: First element is excluded we have to
    # calculate result 2 to n houses.
    ans2 = helper(arr[1:], n - 1)
    return max(ans1, ans2)
 
# Driver to test above code
if __name__ == "__main__":
    arr = [1, 2, 3, 1]
    n = len(arr)
    print("Maximum loot possible : ", maxLoot(arr, n - 1))
 
    # This code is contributed by lokeshpotta20


C#




// Include namespace system
using System;
 
public class GFG
{
  // calculate the maximum stolen value
  public static int helper(int[] arr, int l, 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
    var pick = arr[n] + GFG.helper(arr, l, n - 2);
 
    // if current element is not picked then previous
    // element is picked
    var notPick = GFG.helper(arr, l, n - 1);
 
    // return max of picked and not picked
    return Math.Max(pick,notPick);
  }
  public static int findMaxSum(int[] arr, int n)
  {
 
    // case 1: Last element is excluded we have to calculate
    // result for first n-1 houses.
    var ans1 = GFG.helper(arr, 0, n - 1);
 
    // case 2: First element is excluded we have to
    // calculate result 2 to n houses.
    var ans2 = GFG.helper(arr, 1, n - 1);
    return Math.Max(ans1,ans2);
  }
 
  // Driver Code
  public static void Main(String[] args)
  {
    int[] arr = {1, 2, 3, 1};
    var n = arr.Length;
    Console.WriteLine("Maximum loot possible : " + GFG.findMaxSum(arr, n).ToString());
  }
}
 
// This code is contributed by aadityaburujwale.


Javascript




// calculate the maximum stolen value
function helper(arr, l, n)
{
    // base case
    if (n < 0)
    {
        return 0;
    }
    if (n == 0)
    {
        return arr[0];
    }
     
    // if current element is pick then previous cannot be
    // picked
    var pick = arr[n] + helper(arr, l, n - 2);
     
    // if current element is not picked then previous
    // element is picked
    var notPick = helper(arr, l, n - 1);
     
    // return max of picked and not picked
    return Math.max(pick,notPick);
}
function findMaxSum(arr, n)
{
    // case 1: Last element is excluded we have to calculate
    // result for first n-1 houses.
    var ans1 = helper(arr, 0, n - 1);
     
    // case 2: First element is excluded we have to
    // calculate result 2 to n houses.
    var ans2 = helper(arr, 1, n - 1);
    return Math.max(ans1,ans2);
}
 
// Driver Code
    var arr = [1, 2, 3, 1];
    var n = arr.length;
    console.log("Maximum loot possible : " + findMaxSum(arr, n));
     
    // This code is contributed by sourabhdalal0001.


Output

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




// 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 position
int 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 position
int 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 Code
int main()
{
    int arr[] = { 1, 2, 3, 1 };
    int n = sizeof(arr)/sizeof(arr[0]);
    cout << findMaxSum(arr, n);
    return 0;
}


Java




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




# 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 position
def 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 position
def 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 Code
if __name__ == "__main__":
     
    arr = [ 1, 2, 3, 1 ]
    n = len(arr)
    print(findMaxSum(arr, n))
 
# This code is contributed by ita_c


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 position
static 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 position
static 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
static 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
// 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 position
function 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
?>


Javascript




<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 position
function 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 position
function 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>


Output

4

Time Complexity: O(N^2) 

Auxiliary Space: O(N)

Space optimization approach: As we can see the dp[i] is calculated from previous values so we can just store those previous values in variables.

Implementation:

C++14




// C++ program to find the maximum stolen value
#include <iostream>
using namespace std;
 
// calculate the maximum stolen value
int 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 code
int 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;
}


Java




// Java program to find the maximum stolen value
  
public class GFG {
  
// calculate the maximum stolen value
static int 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 = Math.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 = Math.max(first, second);
  
        value2 = value1;
        value1 = curr;
    }
    max_val2 = value1;
    return Math.max(max_val1, max_val2);
}
  
    // Driver Code
    public static void main (String[] args)
    {
        // Value of houses
        int arr[] = { 6, 7, 1, 3, 8, 2, 4 };
        int n = arr.length;
        System.out.println(findMaxSum(arr, n));
    }
}
// This code is contributed by aditya942003patil


Python3




# C++ program to find the maximum stolen value
 
# calculate the maximum stolen value
def findMaxSum(arr, n):
 
    if n == 0 :
        return 0
 
    value1 = arr[0]
    if n == 1 :
        return value1
 
    value2 = 0
 
    # case 1: check for 1 to n-1 houses.
    # contains maximum stolen value at the end
    max_val1, max_val2 = 0,0
 
    # Fill remaining positions
    for i in range(1,n) :
        first = value1
        second = arr[i]
        if i > 1 :
            second += value2
        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 i in range(2,n) :
        first = value1
        second = arr[i]
        if i > 1 :
            second += value2
        curr = max(first, second)
 
        value2 = value1
        value1 = curr
         
    max_val2 = value1
    return max(max_val1, max_val2)
 
# Driver to test above code
if __name__ == "__main__":
     
    # Value of houses
    arr = [ 6, 7, 1, 3, 8, 2, 4 ]
    n = len(arr)
    print(findMaxSum(arr, n))
 
# This code is contributed by aditya942003patil


C#




// C# program to find the maximum stolen value
 
using System;
using System.Collections;
using System.Collections.Generic;
 
public class GFG {
 
  // calculate the maximum stolen value
  static int 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 = Math.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 = Math.Max(first, second);
 
      value2 = value1;
      value1 = curr;
    }
    max_val2 = value1;
    return Math.Max(max_val1, max_val2);
  }
 
  // Driver Code
  public static void Main(string[] args)
  {
    // Value of houses
    int[] arr = { 6, 7, 1, 3, 8, 2, 4 };
    int n = arr.Length;
    Console.WriteLine(findMaxSum(arr, n));
  }
}
// This code is contributed by karandeep1234.


Javascript




// JS program to find the maximum stolen value
 
 
// calculate the maximum stolen value
function findMaxSum(arr, n) {
    if (n == 0)
        return 0;
 
    let value1 = arr[0];
    if (n == 1)
        return value1;
 
    let value2 = 0;
 
    // case 1: check for 1 to n-1 houses.
    // contains maximum stolen value at the end
    let max_val1, max_val2;
 
    // Fill remaining positions
    for (let i = 1; i < n - 1; i++) {
        let first = value1;
        let second = arr[i];
        if (i > 1) {
            second += value2;
        }
        let curr = Math.max(first, second);
 
        value2 = value1;
        value1 = curr;
    }
    // case 2: check for 2 to n houses.
    max_val1 = value1;
    value1 = arr[1];
    value2 = 0;
    for (let i = 2; i < n; i++) {
        let first = value1;
        let second = arr[i];
        if (i > 1) {
            second += value2;
        }
        let curr = Math.max(first, second);
 
        value2 = value1;
        value1 = curr;
    }
    max_val2 = value1;
    return Math.max(max_val1, max_val2);
}
 
// Driver to test above code
 
 
// Value of houses
let arr = [6, 7, 1, 3, 8, 2, 4];
let n = arr.length;
console.log(findMaxSum(arr, n));
return 0;
 
// This code is contributed by adityamaharshi21


Output

19

Complexity Analysis:

  • Time Complexity:  O(N).
  • Space Complexity: O(1)


Last Updated : 04 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads