Data Structures & AlgorithmsHard
For the array [1, -2, 3, -1, 4, -6, 2], what is the maximum possible sum of a contiguous non-empty subarray after deleting AT MOST one of its elements (the remaining part must be non-empty)?
- A. 6
- B. 4
- C. 8
- D. 1
Correct answer: C. 8
Track the best sum with no deletion and with one deletion: deleting −6 from [3, −1, 4, −6, 2] gives 3 − 1 + 4 + 2 = 8. Plain Kadane (no deletion) gives 6, the largest element is 4 and the whole-array sum is 1.
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
- In the binary array [1, 1, 0, 1, 1, 1, 0, 1], you must delete exactly one element. What is the length of the longest run of consecutive 1s…
- How many index triples i < j < k in the sorted array [-2, 0, 1, 3, 5, 6] have A[i] + A[j] + A[k] strictly less than 4?
- For [2, 1, 3, 5, 4, 6, 8, 7, 9], how many indices i with 1 ≤ i ≤ n−2 satisfy: every element before i is strictly less than a[i], and every…
- How many split points p with 1 ≤ p ≤ n−1 divide [1, 2, 2, 3, 2, 4, 5] into a left part and a right part such that every element of the…
- What is the length of the longest contiguous subarray of [4, 6, 5, 8, 9, 7, 10, 3] in which (maximum − minimum) ≤ 2?
- Choose a subsequence (elements need not be adjacent, but keep their original order) of [1, 9, 3, 4, 8, 2, 10, 5] of largest size such that…