VOOZH about

URL: https://www.geeksforgeeks.org/dsa/maximum-sum-in-circular-array-such-that-no-two-elements-are-adjacent/

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


  • Courses
  • Tutorials
  • Interview Prep

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

Last Updated : 27 Jan, 2026

Given an array arr[] which represents houses arranged in a circle, where each house has a certain value. A thief aims to maximize the total stolen value without robbing two adjacent houses. Since the houses are in a circle, the first and last houses are also considered adjacent. Determine the maximum amount the thief can steal.

Examples:

Input: arr[] = [2, 2, 3, 1, 2]
Output: 5
Explanation: Maximum stolen value: arr[0] + arr[2] = 2 + 3 = 5 or arr[2] + arr[4] = 3 + 2 = 5.

Input: arr[] = [2, 3, 2]
Output: 3
Explanation: arr[0] and arr[2] can't be robbed simultaneously because they are adjacent houses.

[Naive Approach] Using Recursion - O(2^n) Time and O(n) Space

The idea is to solve this problem by handling the circular constraint, where the first and last houses are adjacent.

To avoid robbing both of them together, we divide the problem into two linear cases:

  • Consider houses from index 0 to nβˆ’2 (exclude the last house).
  • Consider houses from index 1 to nβˆ’1 (exclude the first house).

For each case, the thief has two choices at every house:

  • Rob the current house β†’ then skip the adjacent (previous) one and add the value of the current house.
  • Skip the current house β†’ take the maximum value obtained so far.

We solve both cases separately using recursion and then take the maximum of the two results.



Output
5

[Better Approach - 1 ] Using Top-Down DP (Memoization) - O(n) Time and O(n) Space

In the recursive solution, there are many overlapping subproblems because the same states are solved multiple times.
To avoid this repetition, we can use memoization where we store the results of already computed subproblems in a separate array (memo). Before solving any subproblem, we first check in the memo[i] if it has been computed earlier. If yes, we simply reuse the stored result instead of recalculating it.


Output
5

[Better Approach - 2] Using Bottom-Up DP (Tabulation) - O(n) Time and O(n) Space

The main idea is to build the solution iteratively using a DP table where each entry represents the maximum money that can be stolen up to that house.

The relation that helps us compute this is: dp[j] = max(arr[j] + dp[j-2], dp[j-1])
This ensures we always choose the option that gives the maximum total amount without robbing two adjacent houses.

The formula dp[j] = max(arr[j] + dp[jβˆ’2], dp[jβˆ’1]) comes from two choices:

  • The thief robs the current house then skips the previous one, so total becomes arr[j] + dp[jβˆ’2].
  • The thief skips the current house β†’ and takes the maximum money till the previous house, i.e. dp[jβˆ’1].

We take the maximum of these two options to ensure the thief always gets the highest possible amount.


Output
5

[Expected Approach ] Using Space Optimized DP - O(n) Time and O(1) Space

In the previous approach, we used an array to store results for all states. However, if we look closely, to calculate the result for the current house, we only need the values from the previous two houses.This means there’s no need to store all previous results.
We can simply keep track of the last two computed values and update them as we move forward.


Output
5
Comment
Article Tags: