forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubarraySumEqualsK.java
More file actions
72 lines (62 loc) · 1.9 KB
/
SubarraySumEqualsK.java
File metadata and controls
72 lines (62 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package com.thealgorithms.prefixsum;
import java.util.HashMap;
import java.util.Map;
/**
* Implements an algorithm to count the number of continuous subarrays
* whose sum equals a given value k.
*
* <p>
* This algorithm uses the Prefix Sum technique combined with a HashMap
* to achieve O(N) time complexity.
* </p>
*
* <p>
* Let prefixSum[i] be the sum of elements from index 0 to i.
* A subarray (j + 1) to i has sum k if:
*
* <pre>
* prefixSum[i] - prefixSum[j] = k
* </pre>
* </p>
*
* <p>
* The HashMap stores the frequency of each prefix sum encountered so far.
* </p>
*
* <p>
* <strong>Time Complexity:</strong> O(N)<br>
* <strong>Space Complexity:</strong> O(N)
* </p>
*
* @see <a href="https://en.wikipedia.org/wiki/Prefix_sum">Prefix Sum (Wikipedia)</a>
* @author Ruturaj Jadhav, <a href="https://github.com/ruturajjadhav07">ruturajjadhav07</a>
*/
public final class SubarraySumEqualsK {
private SubarraySumEqualsK() {
// Utility class; prevent instantiation
}
/**
* Counts the number of subarrays whose sum equals k.
*
* @param nums The input integer array.
* @param k The target sum.
* @return The number of continuous subarrays summing to k.
* @throws IllegalArgumentException if nums is null.
*/
public static int countSubarrays(int[] nums, int k) {
if (nums == null) {
throw new IllegalArgumentException("Input array cannot be null");
}
Map<Long, Integer> prefixSumFrequency = new HashMap<>();
prefixSumFrequency.put(0L, 1);
long prefixSum = 0;
int count = 0;
for (int num : nums) {
prefixSum += num;
long requiredSum = prefixSum - k;
count += prefixSumFrequency.getOrDefault(requiredSum, 0);
prefixSumFrequency.put(prefixSum, prefixSumFrequency.getOrDefault(prefixSum, 0) + 1);
}
return count;
}
}