Data Structures & AlgorithmsHardCodeforces Div2 C · TCS NQT
Choose any subset of the positions of [4, -2, 7, 3, 9, 1, -5, 8, 2] (possibly empty) so that every two chosen positions are at least 3 apart (indices differ by 3 or more). What is the maximum possible sum?
- A. 22
- B. 17
- C. 21
- D. 18
Correct answer: C. 21
dp[i] = max(dp[i−1], a[i] + dp[i−3]): either skip position i or take it, which forces the previous chosen position to be at most i − 3. Allowing neighbours at distance 2 is a different constraint.
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
- Assertion (A): Dynamic programming over subsets for TSP needs exponential time in the number of cities. Reason (R): The number of subsets…
- Assertion (A): Kadane's algorithm can be used to find the maximum subarray sum in O(n) time. Reason (R): It keeps the best sum ending at…
- Assertion (A): Binary search on the answer requires that the feasibility predicate is monotone. Reason (R): Only a monotone predicate lets…
- Assertion (A): The greedy 'largest coin first' is optimal for the coin set {1, 3, 4} when the amount is 6. Reason (R): A coin system whose…
- Which of the following statements about the sliding window technique on arrays with NEGATIVE numbers is/are correct? I. A plain…
- The array [7, 2, 5, 10, 8, 3, 6] must be split into exactly 3 contiguous non-empty parts (order kept). What is the smallest possible value…