-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBOJ_1940.java
More file actions
40 lines (34 loc) · 1.09 KB
/
Copy pathBOJ_1940.java
File metadata and controls
40 lines (34 loc) · 1.09 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
35
36
37
38
39
40
// BOJ - 1940
// Problem Sheet - https://www.acmicpc.net/problem/1940
import java.util.*;
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
int m = Integer.parseInt(br.readLine());
int[] ingredients = new int[n];
StringTokenizer st = new StringTokenizer(br.readLine());
for(int i=0 ; i<n ; i++) {
ingredients[i] = Integer.parseInt(st.nextToken());
}
int answer = 0;
Arrays.sort(ingredients);
int left = 0, right = n-1;
while(left < right) {
int combination = ingredients[left] + ingredients[right];
if(combination == m) {
answer++;
left++;
right--;
} else if(combination < m) {
left++;
} else {
right--;
}
}
System.out.println(answer);
br.close();
System.exit(0);
}
}