Data Structures & AlgorithmsHardCodeforces Div2 C · TCS NQT

From the array [5, 1, 8, 2, 9, 3, 7, 4], choose exactly 3 elements (by index) so that no two chosen indices are adjacent, and maximise their sum. What is the maximum sum?

  1. A. 21
  2. B. 24
  3. C. 39
  4. D. 25

Correct answer: B. 24

A DP over (index, count) with the rule 'skip the next index after taking one' gives the exact maximum. Greedily taking the largest values can pick adjacent pairs, which the rule forbids.

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

Exams that ask this topic