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?
- A. 21
- B. 24
- C. 39
- 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.
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 inversions does the array [2, 4, 1, 3, 5] have? An inversion is a pair of positions i < j with a[i] > a[j].
- What is the minimum number of elements you must INSERT into [1, 4, 2, 7, 4, 1, 3] (at any positions) so that the array becomes a palindrome?
- How many contiguous subarrays of length at least 2 of [23, 2, 4, 6, 7] have a sum that is a multiple of 6?
- Sum the lengths of all contiguous subarrays of [1, 2, 3, 4] whose element sum is even.
- How many index pairs i < j in [1, 4, 6, 9, 10, 12] satisfy a[i] + a[j] >= 15?
- How many subarrays [i..j] of [3, 1, 4, 1, 5] have their first element strictly greater than every other element of the subarray?