File tree Expand file tree Collapse file tree 1 file changed +50
-0
lines changed
Expand file tree Collapse file tree 1 file changed +50
-0
lines changed Original file line number Diff line number Diff line change 1+ ```
2+ import java.io.*;
3+ import java.util.*;
4+
5+ public class Main {
6+ private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
7+ private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
8+ private static Map<Integer, Integer> map;
9+ private static int[] arr;
10+ private static int N, K;
11+ private static long answer;
12+
13+ public static void main(String[] args) throws IOException {
14+ init();
15+
16+ bw.write(answer + "\n");
17+ bw.flush();
18+ bw.close();
19+ br.close();
20+ }
21+
22+ private static void init() throws IOException {
23+ StringTokenizer st = new StringTokenizer(br.readLine());
24+ N = Integer.parseInt(st.nextToken());
25+ K = Integer.parseInt(st.nextToken());
26+
27+ arr = new int[N];
28+ map = new HashMap<>();
29+
30+ st = new StringTokenizer(br.readLine());
31+ for (int i = 0; i < N; i++) {
32+ arr[i] = Integer.parseInt(st.nextToken());
33+ }
34+
35+ int currentSum = 0;
36+
37+ for (int n : arr) {
38+ currentSum += n;
39+
40+ if (currentSum == K) answer++;
41+
42+ if (map.containsKey(currentSum-K)) {
43+ answer += map.get(currentSum-K);
44+ }
45+
46+ map.put(currentSum, map.getOrDefault(currentSum, 0) + 1);
47+ }
48+ }
49+ }
50+ ```
You can’t perform that action at this time.
0 commit comments