Diwali Offer!Flat 20% OFF
Enroll Now
PrakalpanaLive online tech training

12. Subarray Sum Equals K

MediumDay 3 of 60Arrays & Hashing

Given an integer array nums and an integer k, count how many contiguous non-empty subarrays have a sum exactly equal to k. The array can contain negative numbers and zeros.

Example 1
Input: nums = [1,1,1], k = 2
Output: 2
The subarrays [1,1] starting at index 0 and at index 1 both sum to 2.
Example 2
Input: nums = [1,2,3], k = 3
Output: 2
[1,2] and [3].
Constraints
  • 1 <= nums.length <= 2 * 10^4
  • -1000 <= nums[i] <= 1000
  • -10^7 <= k <= 10^7
Hints
Frequently asked at: Meta, Google, Amazon, Goldman Sachs, Flipkart
Ctrl + Enter runs the examples

Not sure what to learn next?

A 1-on-1 session with an industry mentor and a roadmap built for you. How it works

  1. 1Discuss your goals
  2. 2Get a personal plan
  3. 3Start learning
Book for β‚Ή200/ 30 min