|
| 1 | +--- |
| 2 | +id: subarray-sum-equals-k |
| 3 | +title: Subarray Sum Equals K |
| 4 | +sidebar_label: Subarray Sum Equals K |
| 5 | +tags: |
| 6 | + - Array |
| 7 | + - HashMap |
| 8 | + - Prefix Sum |
| 9 | +--- |
| 10 | + |
| 11 | +## Problem Description |
| 12 | + |
| 13 | +| Problem Statement | Solution Link | LeetCode Profile | |
| 14 | +| :------------------------------------------------------ | :------------------------------------------------------------------------- | :------------------------------------------------------ | |
| 15 | +| [subarray-sum-equals-k](https://leetcode.com/problems/subarray-sum-equals-k/description/) | [subarray-sum-equals-k Solution on LeetCode](https://leetcode.com/problems/subarray-sum-equals-k/solutions/) | [Nikita Saini](https://leetcode.com/u/Saini_Nikita/) | |
| 16 | + |
| 17 | +## Problem Description |
| 18 | + |
| 19 | +Given an array of integers `nums` and an integer `k`, return the total number of subarrays whose sum equals to `k`. |
| 20 | + |
| 21 | +A subarray is a contiguous non-empty sequence of elements within an array. |
| 22 | + |
| 23 | +### Example 1 |
| 24 | + |
| 25 | +**Input**: `nums = [1,1,1], k = 2` |
| 26 | +**Output**: `2` |
| 27 | + |
| 28 | +### Example 2 |
| 29 | + |
| 30 | +**Input**: `nums = [1,2,3], k = 3` |
| 31 | +**Output**: `2` |
| 32 | + |
| 33 | +### Constraints |
| 34 | + |
| 35 | +- `1 <= nums.length <= 2 * 10^4` |
| 36 | +- `-1000 <= nums[i] <= 1000` |
| 37 | +- `-10^7 <= k <= 10^7` |
| 38 | + |
| 39 | +## Approach |
| 40 | + |
| 41 | +To solve this problem efficiently, we can use a HashMap (dictionary in Python) to keep track of the cumulative sum of elements up to the current index and the number of times each cumulative sum has been encountered. This approach allows us to determine the number of subarrays that sum to `k` in linear time. |
| 42 | + |
| 43 | +### Step-by-Step Algorithm |
| 44 | + |
| 45 | +1. Initialize a dictionary `prefix_sum_count` to store the frequency of prefix sums. Start with the prefix sum `0` having a count of `1`. |
| 46 | +2. Initialize `current_sum` to `0` and `count` to `0`. |
| 47 | +3. Iterate through each element in the array: |
| 48 | + - Add the current element to `current_sum`. |
| 49 | + - If `current_sum - k` exists in `prefix_sum_count`, it means there are subarrays ending at the current index that sum to `k`. Add the count of `current_sum - k` to `count`. |
| 50 | + - Increment the count of `current_sum` in `prefix_sum_count`. |
| 51 | +4. Return `count`. |
| 52 | + |
| 53 | +## Solution in Python |
| 54 | + |
| 55 | +```python |
| 56 | +def subarraySum(nums, k): |
| 57 | + prefix_sum_count = {0: 1} |
| 58 | + current_sum = 0 |
| 59 | + count = 0 |
| 60 | + |
| 61 | + for num in nums: |
| 62 | + current_sum += num |
| 63 | + if (current_sum - k) in prefix_sum_count: |
| 64 | + count += prefix_sum_count[current_sum - k] |
| 65 | + if current_sum in prefix_sum_count: |
| 66 | + prefix_sum_count[current_sum] += 1 |
| 67 | + else: |
| 68 | + prefix_sum_count[current_sum] = 1 |
| 69 | + |
| 70 | + return count |
| 71 | +``` |
| 72 | + |
| 73 | +## Solution in Java |
| 74 | + |
| 75 | +```java |
| 76 | +import java.util.HashMap; |
| 77 | +import java.util.Map; |
| 78 | + |
| 79 | +public class Solution { |
| 80 | + public int subarraySum(int[] nums, int k) { |
| 81 | + Map<Integer, Integer> prefixSumCount = new HashMap<>(); |
| 82 | + prefixSumCount.put(0, 1); |
| 83 | + int current_sum = 0; |
| 84 | + int count = 0; |
| 85 | + |
| 86 | + for (int num : nums) { |
| 87 | + current_sum += num; |
| 88 | + if (prefixSumCount.containsKey(current_sum - k)) { |
| 89 | + count += prefixSumCount.get(current_sum - k); |
| 90 | + } |
| 91 | + prefixSumCount.put(current_sum, prefixSumCount.getOrDefault(current_sum, 0) + 1); |
| 92 | + } |
| 93 | + |
| 94 | + return count; |
| 95 | + } |
| 96 | +} |
| 97 | +``` |
| 98 | + |
| 99 | +## Solution in C++ |
| 100 | + |
| 101 | +```cpp |
| 102 | +#include <vector> |
| 103 | +#include <unordered_map> |
| 104 | + |
| 105 | +class Solution { |
| 106 | +public: |
| 107 | + int subarraySum(std::vector<int>& nums, int k) { |
| 108 | + std::unordered_map<int, int> prefixSumCount = {{0, 1}}; |
| 109 | + int current_sum = 0; |
| 110 | + int count = 0; |
| 111 | + |
| 112 | + for (int num : nums) { |
| 113 | + current_sum += num; |
| 114 | + if (prefixSumCount.find(current_sum - k) != prefixSumCount.end()) { |
| 115 | + count += prefixSumCount[current_sum - k]; |
| 116 | + } |
| 117 | + prefixSumCount[current_sum]++; |
| 118 | + } |
| 119 | + |
| 120 | + return count; |
| 121 | + } |
| 122 | +}; |
| 123 | +``` |
| 124 | +
|
| 125 | +## Solution in C |
| 126 | +
|
| 127 | +```c |
| 128 | +#include <stdio.h> |
| 129 | +#include <stdlib.h> |
| 130 | +
|
| 131 | +int subarraySum(int* nums, int numsSize, int k) { |
| 132 | + int *prefixSumCount = (int*)calloc(numsSize * 2000, sizeof(int)); // Assuming numsSize <= 20000 |
| 133 | + prefixSumCount[numsSize] = 1; // Initial prefix sum 0 |
| 134 | + |
| 135 | + int current_sum = 0; |
| 136 | + int count = 0; |
| 137 | + |
| 138 | + for (int i = 0; i < numsSize; i++) { |
| 139 | + current_sum += nums[i]; |
| 140 | + int target_sum = current_sum - k; |
| 141 | + count += prefixSumCount[target_sum + numsSize]; |
| 142 | + prefixSumCount[current_sum + numsSize]++; |
| 143 | + } |
| 144 | + |
| 145 | + free(prefixSumCount); |
| 146 | + return count; |
| 147 | +} |
| 148 | +``` |
| 149 | + |
| 150 | +## Solution in JavaScript |
| 151 | + |
| 152 | +```javascript |
| 153 | +var subarraySum = function(nums, k) { |
| 154 | + let prefixSumCount = {0: 1}; |
| 155 | + let current_sum = 0; |
| 156 | + let count = 0; |
| 157 | + |
| 158 | + for (let num of nums) { |
| 159 | + current_sum += num; |
| 160 | + if (prefixSumCount[current_sum - k] !== undefined) { |
| 161 | + count += prefixSumCount[current_sum - k]; |
| 162 | + } |
| 163 | + prefixSumCount[current_sum] = (prefixSumCount[current_sum] || 0) + 1; |
| 164 | + } |
| 165 | + |
| 166 | + return count; |
| 167 | +}; |
| 168 | +``` |
| 169 | + |
| 170 | +## Conclusion |
| 171 | + |
| 172 | +The described algorithm efficiently finds the number of subarrays that sum up to a given value `k` using a HashMap to store prefix sums. This approach ensures that the solution runs in linear time, making it suitable for large input sizes. |
0 commit comments