Data Structures & AlgorithmsModerate
In the array [2, 3, 1, 2, 4, 3] (all positive), what is the length of the shortest contiguous subarray whose sum is at least 7?
- A. 4
- B. 3
- C. 2
- D. 6
Correct answer: C. 2
For positive values the shortest qualifying window is found by growing the right end and shrinking the left while the sum stays ≥ S. The first window met in scan order need not be the shortest, and the strict > bound gives a different answer.
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 contiguous subarrays of [1, 5, 2, 8, 3, 7] have a sum divisible by their own length?
- For [2, 3, 4, 6, 9, 5, 12], how many index pairs i < j have the product BB[i]·BB[j] divisible by 6?
- How many contiguous subarrays of [5, 5, 3, 3, 6, 6, 6] have XOR equal to 0?
- How many index pairs i < j in [1, 5, 3, 3, 3, 7, 2, 4, 6] have DD[i] + DD[j] = 6? Equal values at different indices are different pairs.
- How many contiguous subarrays of [2, 1, 3, 1, 1, 2, 4, 1] have each element sum equal to its length (sum of (a[i] − 1) over the subarray…
- How many contiguous subarrays of [1, 2, 1, 2, 3, 3, 4] contain exactly 2 distinct values?