You see "subarray" in the problem. You immediately think: "Prefix sum!"
You implement it. Wrong Answer or Time Limit Exceeded.
What went wrong? Not every subarray problem is a prefix sum problem. Some need Kadane's algorithm, sliding window, or dynamic programming instead.
In this guide, you'll learn when prefix sum is the wrong choice and what to use instead.
TL;DR
Prefix sum fails when:
- Finding maximum/minimum subarray → Use Kadane's
- Longest substring with condition → Use sliding window
- Dynamic updates to array → Use segment tree
- Non-additive operations → Use other techniques
Problem 1: Maximum Subarray (Kadane's Algorithm)
Why Prefix Sum Fails
Problem: Find maximum sum of any contiguous subarray.
Prefix sum approach (WRONG):
# This doesn't work!
prefix = [0]
for num in nums:
prefix.append(prefix[-1] + num)
# How do you find max? Check all pairs?
max_sum = float('-inf')
for i in range(len(nums)):
for j in range(i, len(nums)):
max_sum = max(max_sum, prefix[j+1] - prefix[i])
# Time: O(n²) - too slow!What to Use Instead: Kadane's Algorithm
def maxSubArray(nums):
max_sum = current_sum = nums[0]
for num in nums[1:]:
current_sum = max(num, current_sum + num)
max_sum = max(max_sum, current_sum)
return max_sum
# Time: O(n), Space: O(1)Why it works: Kadane's tracks the maximum ending at each position, not cumulative sums.
Problem 2: Longest Substring Without Repeating Characters
Why Prefix Sum Doesn't Apply
This isn't a sum problem!
Problem: Find longest substring with all unique characters.
What to Use Instead: Sliding Window
def lengthOfLongestSubstring(s):
left = 0
seen = set()
max_len = 0
for right in range(len(s)):
while s[right] in seen:
seen.remove(s[left])
left += 1
seen.add(s[right])
max_len = max(max_len, right - left + 1)
return max_lenWhy: Sliding window handles variable-size windows with conditions.
Problem 3: Range Sum Query with Updates
Why Static Prefix Sum Fails
Problem: Support both range sum queries AND updates.
Prefix sum approach (WRONG):
# Update: O(n) to rebuild entire prefix array
# Too slow for many updates!What to Use Instead: Segment Tree or Fenwick Tree
# Segment Tree: O(log n) update, O(log n) query
# Fenwick Tree: O(log n) update, O(log n) queryWhy: These data structures support efficient updates, prefix sum doesn't.
Pattern Recognition: When to Avoid Prefix Sum
✅ Prefix Sum Works
- Range queries on static array
- Exact target sum
- Counting subarrays
❌ Prefix Sum Fails
- Maximum/minimum → Kadane's or DP
- Longest/shortest with positives → Sliding window
- Frequent updates → Segment tree
- Non-sum operations → Other techniques
Conclusion
Key takeaways:
- Maximum subarray → Kadane's algorithm
- Longest substring → Sliding window
- Dynamic updates → Segment tree
- Not all subarray problems need prefix sum
For more, see Prefix Sum vs Sliding Window and the complete guide.
Want to Practice LeetCode Smarter?
LeetCopilot is a free browser extension that enhances your LeetCode practice with AI-powered hints, personalized study notes, and realistic mock interviews — all designed to accelerate your coding interview preparation.
Also compatible with Edge, Brave, and Opera
