Data Structures & AlgorithmsVery hardCodeforces Div2 C · TCS NQT

Choose exactly 4 elements of [6, 1, 9, 4, 7, 2, 8, 5] (by position) such that no two chosen positions are adjacent. What is the maximum possible sum?

  1. A. 33
  2. B. 24
  3. C. 30
  4. D. 31

Correct answer: C. 30

A DP over (index, count) where taking index i forces skipping i+1 gives the optimum. Taking the k largest values ignores the adjacency rule, and using k − 1 elements is a different count.

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

Exams that ask this topic