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?
- A. 33
- B. 24
- C. 30
- 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.
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
- How many permutations of 1, 2, 3, 4, 5 have exactly 3 inversions (pairs of positions i < j with p[i] > p[j])?
- What is the minimum number of positive perfect cubes (1, 8, 27, ...) whose sum is exactly 31? Repeats are allowed.
- How many substrings of "abacaxayaba" are palindromes of ODD length at least 3?
- Among all distinct permutations of the letters of "dbca" sorted alphabetically, what is the 1-based position of "dbca" itself?
- List all distinct permutations of the multiset of letters in "aabc" in alphabetical order. What is the 5th string in this list?
- What is the length of the longest substring of "aabbcabccbba" in which every letter occurs an EVEN number of times (0 counts as even)?