Count Subarrays With Exact Sum K (Negatives Allowed)
Difficulty: Hard · Unit 3: Built-in Data Structures & Hashing · Featured in: Tough Challenge
Given an integer array of length N (which may include negative numbers and zeros) and a target integer K, count the total number of contiguous non-empty subarrays whose elements sum exactly to K.
Input & Output Format
Input: Line 1: two integers N and K. Line 2: N space-separated integers.
Output: Print a single integer count.
Sample 1
5 3 1 2 3 -3 3
5
Subarrays summing to 3: [1,2], [1,2,3,-3], [3], [3,-3,3], and final [3].
Free forever — sign in with email at poorvithmp.com to unlock the dry run, verified Python 3 solution, edge-case traps, and downloads.