1/30
Loading...
🎯Array with Negatives!
Find all contiguous subarrays with sum K=5 in [2, 3, -1, 1, 5]! -1 means sliding window won't work!
Loading...
Find all contiguous subarrays with sum K=5 in [2, 3, -1, 1, 5]! -1 means sliding window won't work!
You are analyzing daily sales data for a store. Find how many contiguous periods have a total sales sum exactly equal to the target amount K. Negative values represent refunds or losses.
sales = [1, 2, 3, -2, 5], K = 5
3
Subarrays with sum 5: • [2, 3] → 2+3=5 ✓ • [5] → 5 ✓ • Index 1~2: 2+3=5 ✓ • Index 4: 5 ✓ Total: 2 (Corrected example) sales = [1, 1, 1], K = 2 • [1,1] (index 0~1) ✓ • [1,1] (index 1~2) ✓ Total: 2!
sales = [2, 3, -1, 1, 5], K = 5
4
Subarrays with sum 5: • [2, 3] = 5 ✓ • [2, 3, -1, 1] = 5 ✓ (includes negative!) • [-1, 1, 5] = 5 ✓ • [5] = 5 ✓ Total: 4!