diff --git "a/Ukj0ng/202601/21 BOJ G4 \354\210\230\353\223\244\354\235\230 \355\225\251 4.md" "b/Ukj0ng/202601/21 BOJ G4 \354\210\230\353\223\244\354\235\230 \355\225\251 4.md" new file mode 100644 index 00000000..2acaab92 --- /dev/null +++ "b/Ukj0ng/202601/21 BOJ G4 \354\210\230\353\223\244\354\235\230 \355\225\251 4.md" @@ -0,0 +1,50 @@ +``` +import java.io.*; +import java.util.*; + +public class Main { + private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); + private static Map map; + private static int[] arr; + private static int N, K; + private static long answer; + + public static void main(String[] args) throws IOException { + init(); + + bw.write(answer + "\n"); + bw.flush(); + bw.close(); + br.close(); + } + + private static void init() throws IOException { + StringTokenizer st = new StringTokenizer(br.readLine()); + N = Integer.parseInt(st.nextToken()); + K = Integer.parseInt(st.nextToken()); + + arr = new int[N]; + map = new HashMap<>(); + + st = new StringTokenizer(br.readLine()); + for (int i = 0; i < N; i++) { + arr[i] = Integer.parseInt(st.nextToken()); + } + + int currentSum = 0; + + for (int n : arr) { + currentSum += n; + + if (currentSum == K) answer++; + + if (map.containsKey(currentSum-K)) { + answer += map.get(currentSum-K); + } + + map.put(currentSum, map.getOrDefault(currentSum, 0) + 1); + } + } +} +```