Back to problems

Subarray Sum Equals K

Algorithm · Apple · Medium

Given an integer array nums and an integer k, determine how many non-empty contiguous subarrays have a sum of exactly k. A subarray consists of consecutive elements from the array, and the array can include both negative values and zeros. Examples Example 1: Input: nums = [1,1,1], k = 2 Output: 2 Explanation: The ranges at indices 0..1 and 1..2 each form [1,1], and both have sum 2. Example 2: Input: nums = [1,2,3], k = 3 Output: 2 Explanation: Both [1,2] and [3] add up to 3.…

Checking your access…