FunctionRacerProblemsRace

Subarray Sum Equals K

MediumHash mapsPython, JavaScript, TypeScript, Java, C++

Count the contiguous, non-empty stretches of nums whose items add up to exactly k.

Stretches at different positions count separately, even if they hold the same numbers.

Example 1

nums [1,1,1]
k 2
returns 2

Two stretches: indices 0 to 1 and indices 1 to 2.

Example 2

nums [1,2,3]
k 3
returns 2

The stretches [1, 2] and [3].

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Hash maps problems