LeetCode 560 Subarray Sum Equals K - Why Naive Fails

The trap

Check all subarrays. For each starting index i and ending index j, compute the sum and check if it equals k.

There are O(n2)O(n^2) subarrays. Even with prefix sums (making each sum O(1)O(1)), the total is O(n2)O(n^2).

For n=2×104n = 2 \times 10^4, that's 4×1084 \times 10^8 operations. Too slow.