File tree Expand file tree Collapse file tree 1 file changed +48
-0
lines changed
Expand file tree Collapse file tree 1 file changed +48
-0
lines changed Original file line number Diff line number Diff line change 1+ ``` java
2+ import java.io.* ;
3+ import java.util.* ;
4+
5+ public class Main {
6+ public static void main (String [] args ) throws IOException {
7+ BufferedReader br = new BufferedReader (new InputStreamReader (System . in));
8+ int N = Integer . parseInt(br. readLine(). trim());
9+ long [] arr = new long [N ];
10+ StringTokenizer st = new StringTokenizer (br. readLine());
11+ for (int i = 0 ; i < N ; i++ ) {
12+ arr[i] = Long . parseLong(st. nextToken());
13+ }
14+
15+ Arrays . sort(arr);
16+
17+ int goodCount = 0 ;
18+ for (int k = 0 ; k < N ; k++ ) {
19+ long target = arr[k];
20+ int left = 0 ;
21+ int right = N - 1 ;
22+
23+ while (left < right) {
24+ if (left == k) {
25+ left++ ;
26+ continue ;
27+ }
28+ if (right == k) {
29+ right-- ;
30+ continue ;
31+ }
32+
33+ long sum = arr[left] + arr[right];
34+ if (sum == target) {
35+ goodCount++ ;
36+ break ;
37+ } else if (sum < target) {
38+ left++ ;
39+ } else {
40+ right-- ;
41+ }
42+ }
43+ }
44+
45+ System . out. println(goodCount);
46+ }
47+ }
48+ ```
You can’t perform that action at this time.
0 commit comments