Data Structures & AlgorithmsModerate
Among all non-empty contiguous subarrays of 2 -1 4 -3 5 1, what is the largest sum that does NOT exceed 6?
- A. 6
- B. 8
- C. 1
- D. 12
Correct answer: A. 6
Keep prefix sums sorted and, for each P_j, binary-search the smallest earlier prefix ≥ P_j − 6. Sums of exactly 6 exist: [−1, 4, −3, 5, 1], [4, −3, 5] and [5, 1]. Unconstrained Kadane gives 8 (the whole array), and 12 adds all the positives.
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 how many ways can 1 2 2 0 3 2 1 2 2 be cut into exactly THREE non-empty contiguous parts whose sums are all equal? (A cut is a pair of…
- What is the maximum AVERAGE over all contiguous subarrays of 1 12 -5 -6 50 3 that have length at least 3? Give the answer as a fraction in…
- Choose two NON-OVERLAPPING contiguous windows of length exactly 2 from 4 -2 7 1 -5 6 3 -1. What is the largest possible total sum of their…
- How many contiguous subarrays of 3 1 2 4 0 contain EXACTLY 2 inversions (pairs i < j inside the subarray with a[i] > a[j])?
- What is the length of the longest contiguous subarray of 1 3 5 4 2 6 8 7 3 1 that is bitonic, meaning it strictly increases up to one peak…
- How many contiguous subarrays of 2 1 4 7 3 2 5 3 1 are mountains? A mountain has length at least 3, strictly increases to a single peak…