Data Structures & AlgorithmsModerateTCS NQT · Accenture

Problem: From the array [3, 2, 7, 10], choose a subset with no two adjacent indices chosen, maximising the sum. What is the maximum sum?

  1. A. 13
  2. B. 12
  3. C. 17
  4. D. 22

Correct answer: A. 13

dp gives max(3 + 10, 2 + 10, 3 + 7) = 13. Taking the odd indices gives 12, taking the adjacent 7 + 10 gives 17 (not allowed), and 22 sums everything.

More Coding Interview Problems questions

Exams that ask this topic