Data Structures & AlgorithmsVery hard
Why does the sliding-window approach fail to find the shortest subarray with sum divisible by m when values are NEGATIVE?
- A. Negative values break the monotonicity of window sums, so shrinking the window cannot guarantee the residue moves toward zero; prefix residues with a hash table are used instead.
- B. Negative numbers cannot be divided by m.
- C. The window always has the largest sum when values are negative.
- D. Sliding windows cannot handle more than one negative value.
Correct answer: A. Negative values break the monotonicity of window sums, so shrinking the window cannot guarantee the residue moves toward zero; prefix residues with a hash table are used instead.
With negatives, the window sum is not monotone in its endpoints, so a one-pass window cannot decide which side to move. Prefix residues with first-occurrence positions solve the same 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
- Which property makes the ordered-pair count of A[i] > A[j] equal to the number of unequal unordered pairs?
- When an array's values are all distinct, what is the number of ordered pairs (i, j), i ≠ j, with A[i] > A[j]?
- Push and pop operations on an initially empty stack are: push, push, pop, push, push, push, pop, pop, push. What is the maximum height the…
- Customers arrive at times 0, 1, 2 and 5 and need service times 3, 2, 1 and 2 on a single server (first come, first served; a customer…
- For the same four customers (arrivals 0, 1, 2, 5; service times 3, 2, 1, 2) on a single first-come-first-served server, what is the…
- A list holds 1 to 9 in order. The first 4 nodes are reversed, then the node with value 6 is deleted. What is the value at position 5 of…