File tree Expand file tree Collapse file tree 1 file changed +71
-0
lines changed
Expand file tree Collapse file tree 1 file changed +71
-0
lines changed Original file line number Diff line number Diff line change 1+ ``` java
2+ import java.io.BufferedReader ;
3+ import java.io.IOException ;
4+ import java.io.InputStreamReader ;
5+ import java.util.Arrays ;
6+ import java.util.StringTokenizer ;
7+
8+ public class BJ_1253_ 좋다 {
9+
10+ private static final BufferedReader br = new BufferedReader (new InputStreamReader (System . in));
11+ private static StringTokenizer st;
12+
13+ private static int N , cnt;
14+ private static long [] arr;
15+
16+ public static void main (String [] args ) throws IOException {
17+ init();
18+ sol();
19+ }
20+
21+ private static void init () throws IOException {
22+ N = Integer . parseInt(br. readLine());
23+ cnt = 0 ;
24+
25+ arr = new long [N ];
26+ st = new StringTokenizer (br. readLine());
27+ for (int i = 0 ; i < N ; i++ ) {
28+ arr[i] = Long . parseLong(st. nextToken());
29+ }
30+ Arrays . sort(arr);
31+ }
32+
33+ private static void sol () {
34+ for (int i = 0 ; i < N ; i++ ) {
35+ if (isGood(i)) {
36+ cnt++ ;
37+ }
38+ }
39+ System . out. println(cnt);
40+ }
41+
42+ private static boolean isGood (int idx ) {
43+ long target = arr[idx];
44+ int left = 0 ;
45+ int right = N - 1 ;
46+
47+ while (left < right) {
48+ if (left == idx) {
49+ left++ ;
50+ continue ;
51+ } else if (right == idx) {
52+ right-- ;
53+ continue ;
54+ }
55+
56+ long current = arr[left] + arr[right];
57+
58+ if (current == target) {
59+ return true ;
60+ } else if (current < target) {
61+ left++ ;
62+ } else {
63+ right-- ;
64+ }
65+ }
66+
67+ return false ;
68+ }
69+
70+ }
71+ ```
You can’t perform that action at this time.
0 commit comments