Count subarrays summing to k=3 in arr=[1,2,1,2,1]. Use prefix sums with a hashmap. As you compute prefix sums, track how many times each sum occurred.
At each position, add count[prefix−k] to the answer. Prefix sums: 0,1,3,4,6,7. At sum 3: count[0]=1, add 1. At sum 4: count[1]=1, add 1. At sum 6: count[3]=1, add 1. At sum 7: count[4]=1, add 1. Total: 4 subarrays. They are: [1,2], [2,1], [1,2], [2,1].