-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path연속_부분수열.java
43 lines (31 loc) · 890 Bytes
/
연속_부분수열.java
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
package 인프런.Section03;
import java.util.Scanner;
public class 연속_부분수열 {
public static int solution(int n, int m, int[] arr) {
int answer = 0, tmp = 0;
int lt = 0;
for(int rt = 0; rt < n; rt++) {
tmp += arr[rt];
if(tmp == m)
answer++;
while(tmp >= m) {
tmp -= arr[lt++];
if(tmp == m)
answer++;
}
}
if(tmp == m)
answer++;
return answer;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int m = scanner.nextInt();
int[] arr = new int[n];
for(int i = 0; i < n; i++) {
arr[i] = scanner.nextInt();
}
System.out.print(solution(n, m, arr));
}
}