data-structures · intermediate · ~15 min
Maximize a non-adjacent sum around a circle.
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.
Non-negative a, count n.
Maximum non-adjacent sum on a circle.
Reduce to two linear passes.
#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; }
Handling the first-last adjacency ad hoc instead of splitting into two linear cases.
n==1 -> a[0].
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.