Subarray Sum Equals K

Medium Solved

Description

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

Input format / Clarification:

To keep the runner simple, input is provided as:

  • Line 1: JSON array of integers (nums)
  • Line 2: Integer k

Examples

Input:

[1,1,1] 2

Output: 2

Input:

[1,2,3] 3

Output: 2

Explanation:

In the first example, the subarrays [1,1] (starting at index 0) and [1,1] (starting at index 1) both sum to 2.

Note:

Your program must print only a single integer - the total count of valid subarrays - so it can be compared with the expected output.

No submissions yet.

Discuss brute force vs prefix-sum + hashmap approaches, time complexity, and handling of negative numbers.

Test Cases