2dbi
Home/HP Inc/Find All Subarrays With a Given Sum
HHP Inc·DSASDE-1Technical Phone Screen

Find All Subarrays With a Given Sum

Problem

Count contiguous subarrays summing to a target (array may contain negatives).

Example

[1,2,3], target=3 -> 2 ([1,2],[3])

Constraints

  • 1 ≤ n ≤ 10^4

Approach

Prefix-sum + hash map.

added 6 days ago
LeadersAccount