Data Structures & AlgorithmsHard
Consider the windows of length 3 of 1 2 3 1 2 4 1 2 starting at positions i and i+1. How many i satisfy sum(window i) = sum(window i+1)?
- A. 4
- B. 1
- C. 5
- D. 3
Correct answer: A. 4
Two consecutive windows of length 3 have equal sums exactly when the entering element equals the leaving element, so this reduces to a comparison of A[i] with A[i+3]. Checking strict inequality or the wrong window pair answers a different question.
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 3 2 5 4 6 1 2 of length at least 3 have exactly one interior strict local maximum (a position strictly…
- How many index pairs i < j in 2 5 3 9 4 7 6 satisfy A[i] < A[j] AND (A[j] − A[i]) divides (j − i)?
- How many contiguous subarrays of length exactly 2 in 2 4 7 9 1 3 6 8 consist of two values with the SAME parity?
- How many index triples i < j < k in 1 2 2 3 4 6 8 satisfy A[i] · A[j] = A[k]?
- How many contiguous subarrays of 3 3 2 4 1 2 have a sum EXACTLY equal to (their length) × (their minimum)?
- Why does the OR of a subarray change at most about log(maximum value) times as its right end extends from a fixed start?