LeetCode 523 Continuous Subarray Sum - Why Naive Fails

The trap

Check all subarrays of length at least 2. For each, compute the sum and check if it's divisible by k.

There are O(n2)O(n^2) subarrays. Even with prefix sums making each sum O(1)O(1), that's still O(n2)O(n^2).

For n=105n = 10^5, that's 101010^{10} operations. Too slow.