Data Structures & AlgorithmsHardCodeforces Div2 C · TCS NQT
What is the maximum sum of a non-empty contiguous subarray of [4, -6, 3, -1, 7, -2, 5] that contains AT MOST one negative element?
- A. 10
- B. 7
- C. 12
- D. 11
Correct answer: A. 10
Let the window hold at most one negative: track the best sums with zero and one negative inside. Allowing none gives a smaller answer, and allowing two gives a larger one.
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
- What is the maximum product of exactly 3 consecutive elements of [2, -3, 4, -1, 5, -6, 2]?
- How many contiguous subarrays (of length at least 2) of [1, 3, 4, 2, 5, 6, 8, 1] are strictly increasing?
- How many subarrays (i ≤ j) of [2, 1, 2, 3, 1, 2] have their first and last elements equal? A single element counts (its first and last are…
- Closed integer intervals are [(1, 4), (2, 6), (5, 9), (3, 3), (6, 8), (7, 12)]. What is the largest number of intervals that share a…
- How many indices i of [2, 2, 0, 2, 2] have the sum of the elements strictly to the left of i equal to the sum of the elements strictly to…
- From the list 1 to 12, every node whose value is a perfect square (1, 4, 9) is deleted. What is the sum of the remaining values?