-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJ1253.java
More file actions
47 lines (36 loc) · 1.12 KB
/
Copy pathJ1253.java
File metadata and controls
47 lines (36 loc) · 1.12 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
41
42
43
44
45
46
47
package TIL;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
public class J1253 {
public static void main(String[] args) throws IOException {
BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in));
int n;
n = Integer.parseInt(buffer.readLine());
long[] num = new long[n];
String[] input = buffer.readLine().split(" ");
for(int i = 0; i < n; i++) {
num[i] = Long.parseLong(input[i]);
}
Arrays.sort(num);
int ans=0;
for(int i = 0; i<n; i++ ) {
long key = num[i];
int from = 0;
int to = n-1;
while (from < to) {
long sum = num[from] + num[to];
if(i == from){
from++; continue;
} else if(to == i){
to--; continue;
}
if (sum > key) to--;
else if (sum < key) from++;
else { ans++; break; }
}
}
System.out.println(ans);
}
}