🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← 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?

  1. A.Sliding window
  2. B.Prefix sums with a hash map
  3. C.Sorting then two pointers
  4. 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 →