-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram120.java
More file actions
34 lines (25 loc) · 1.1 KB
/
Program120.java
File metadata and controls
34 lines (25 loc) · 1.1 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
import java.util.*;
public class Program120 {
//Given an array of integers arr[] and an integer K, return the number of continuous subarrays whose sum equals K.
public static int countSubarraysWithSumK(int[] arr, int K) {
Map<Integer, Integer> prefixSumCount = new HashMap<>();
int currentSum = 0;
int count = 0;
prefixSumCount.put(0, 1); // Important: to handle subarray from index 0
for (int num : arr) {
currentSum += num;
// Check if there's a prefix sum that matches currentSum - K
if (prefixSumCount.containsKey(currentSum - K)) {
count += prefixSumCount.get(currentSum - K);
}
// Update the prefix sum frequency
prefixSumCount.put(currentSum, prefixSumCount.getOrDefault(currentSum, 0) + 1);
}
return count;
}
public static void main(String[] args) {
int[] arr = {1, 2, 3};
int K = 3;
System.out.println("Number of subarrays with sum " + K + ": " + countSubarraysWithSumK(arr, K));
}
}