-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMain.java
40 lines (32 loc) Β· 975 Bytes
/
Main.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
package TimeComplexity.P2003;
import java.io.FileInputStream;
import java.util.Scanner;
public class Main {
static int N, M;
static int[] nums;
static int low = 0, high = 0;
static int hit = 0;
public static void main(String[] args) throws Exception {
System.setIn(new FileInputStream("src/TimeComplexity/P2003/input.txt"));
Scanner sc = new Scanner(System.in);
N = Integer.parseInt(sc.next());
M = Integer.parseInt(sc.next());
nums = new int[N];
for (int i = 0; i < N; i++) {
nums[i] = Integer.parseInt(sc.next());
}
while (low < N && high < N) {
int sum = 0;
for (int i = low; i <= high; i++)
sum += nums[i];
if (sum == M) {
hit++;
high++;
}
else if (sum < M)
high ++;
else low ++;
}
System.out.println(hit);
}
}