Subarray Sum Equals K

Medium · Prefix Sums · Array · Hash Table · Prefix Sum

Problem

Return how many contiguous subarrays of nums sum to exactly k.

Values may be negative, so a running sum does not only increase.

Examples

Input: nums = [1,1,1], k = 2
Output: 2

The two overlapping [1,1] windows both count.

Input: nums = [1,2,3], k = 3
Output: 2

[1,2] and [3].

Constraints

Approaches

Related problems

Start free on Cariara · Coding problems · System design studio

Starting Python
No results yet.

          
↑↓ move ↵ open esc close

Labs

Loading labs…

Run with your own input

Starting Python

Loading Python…