← Arrays & Hashing Quiz: all 8 questions
Arrays & Hashing Quiz · Question 8 of 8
For "count subarrays with sum = k" where the array has NEGATIVE numbers, which approach works?
For "count subarrays with sum = k" where the array has NEGATIVE numbers, which approach works?
- A.Sliding window
- B.Prefix sums with a hash map
- C.Sorting then two pointers
- D.Binary search
Answer & explanation
Correct answer: Prefix sums with a hash map
With negatives, growing a window can lower the sum, so sliding window breaks. Counting earlier prefix sums equal to current − k works for any numbers.
Can you get the rest right without peeking?
Take the timed Arrays & Hashing Quiz quiz free. Pass it to earn XP and count towards your certificate.
Start the quiz →