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?
- A. 22
- B. 27
- C. 20
- 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.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- A staircase has 8 steps with costs [25, 14, 7, 32, 2, 25, 28, 39]. You may start on step 0 or step 1, and each move climbs 1 or 2 steps…
- In how many different ORDERED ways can 10 be written as a sum of parts taken from {1, 3, 4}? Sums that differ only in the order of parts…
- Each digit string maps to letters with 1 to A ... 26 to Z. The string "2262012" is split into groups of one or two digits, and each group…
- For the array [3, -7, 4, -2, 5, -9, 6, 1], you may delete at most ONE element (the rest keep their order). Then you take a non-empty…
- Array [2, 3, 1, 1, 4, 2, 1, 3, 1, 2]: from index i you may jump to any index in the range i+1 ... i+arr[i]. You start at index 0 and want…
- You split the array [1, 15, 7, 9, 2, 5, 10] into contiguous non-empty segments, each of length at most 3. A segment contributes (its…