DevHireLab
TutorialsBootcamp
Problems
Code SimulatorAI InterviewSoonContact
DevHireLab
TutorialsBootcamp
Problems
Code SimulatorAI InterviewSoonContact
Back to Arena
medium
Array

Subarray Sum Equals K

**Problem Statement:** Given an array of integers `nums` and an integer `k`, return the total number of subarrays whose sum equals to `k`. A subarray is a contiguous non-empty sequence of elements within an array. **Hint / Expected Approach:** Prefix sum + hash map **Edge Cases to Consider:** - (1) k = 0 - (2) Negative numbers - (3) Single element equal to k

Examples

Example 1
Input: [1,1,1], 2
Output: 2
Example 2
Input: [1,2,3], 3
Output: 2
Example 3
Input: [1], 0
Output: 0

Constraints

  • ▪1 <= nums.length <= 10^4
  • ▪-10^9 <= nums[i] <= 10^9
  • ▪Time complexity should be O(n) or O(n log n)

Watch Out For Edge Cases

  • ▪k = 0
  • ▪Negative numbers
  • ▪Single element equal to k
Frequently Asked At
GoogleAmazonMetaMicrosoftFlipkart