← Practice · Hash map

Subarray Sum Equals K Medium

Given an integer array nums (may contain negatives and zeros) and an integer k, return the number of contiguous subarrays whose sum equals k.

Examples

  • nums = [1,1,1], k = 2 -> 2
  • nums = [1,2,3], k = 3 -> 2

Define a function named subarraySum in JavaScript.

00:00▶ Visualize

JavaScript · Tab indents · Ctrl/⌘+Enter runs the tests