PIXELBANKv9.1.0
Menu

Subarray Sum Equals K

Given an array of integers nums and an integer k, return the total number of subarrays whose sum equals k.

Example:

Input:
1,1,1
2
Output:
2
Reasoning:
  • The input array is [1, 1, 1] and the target sum k is 22.
  • We can calculate the sum of all possible subarrays: [1] sums to 11, [1, 1] sums to 22, [1, 1, 1] sums to 33, and [1, 1] and [1, 1] (the second and third elements) also sum to 22.
  • The subarrays that sum to k=2k = 2 are [1, 1] (first and second elements) and [1, 1] (second and third elements).
  • The total number of subarrays whose sum equals kk is 22.

Constraints:

  • 1 <= len(nums) <= 2 * 10^4
  • -1000 <= nums[i] <= 1000
  • -10^7 <= k <= 10^7
solution.py

Test Results

0/0
Run code to see test results.
Subarray Sum Equals K - Medium | PixelBank