Data Structures & AlgorithmsVery hardCodeforces Div2 C · GATE CS

Houses stand in a circle with values [5, 1, 9, 8, 4, 6, 2, 7]. Adjacent houses cannot both be robbed, and the first and last houses are adjacent. What is the maximum amount that can be robbed?

  1. A. 22
  2. B. 27
  3. C. 20
  4. D. 15

Correct answer: A. 22

The first and last houses cannot both be taken, so the answer is the better of two linear problems: one on a[:-1] and one on a[1:]. Running the linear rule on the whole circle ignores that adjacency and overcounts.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic