data-structures · intermediate · ~15 min

House robber (circle)

Maximize a non-adjacent sum around a circle.

Challenge

Implement:

long long rob_circular(const int *a, int n);

Same as the linear version, but the houses form a circle: index 0 and n-1 are adjacent.

Input format

Non-negative a, count n.

Output format

Maximum non-adjacent sum on a circle.

Constraints

Reduce to two linear passes.

Starter code

#include <stddef.h>
/* Houses in a circle: element 0 and n-1 are adjacent. Max non-adjacent sum (non-negative). */
long long rob_circular(const int *a,int n){ (void)a;(void)n; return 0; }

Common mistakes

Handling the first-last adjacency ad hoc instead of splitting into two linear cases.

Edge cases to handle

n==1 -> a[0].

Background lessons

Solve this exercise in the browser editor — compile and run against the test harness, no setup required.