File tree Expand file tree Collapse file tree 1 file changed +58
-0
lines changed
Expand file tree Collapse file tree 1 file changed +58
-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+
9+ int n = Integer.parseInt(br.readLine());
10+ long[] arr = new long[n];
11+
12+ StringTokenizer st = new StringTokenizer(br.readLine());
13+ for (int i = 0; i < n; i++) {
14+ arr[i] = Long.parseLong(st.nextToken());
15+ }
16+
17+ Arrays.sort(arr);
18+
19+ int count = 0;
20+ for (int i = 0; i < n; i++) {
21+ if (isGood(arr, n, i)) {
22+ count++;
23+ }
24+ }
25+
26+ System.out.println(count);
27+ }
28+
29+ static boolean isGood(long[] arr, int n, int target) {
30+ int left = 0;
31+ int right = n - 1;
32+ long targetValue = arr[target];
33+
34+ while (left < right) {
35+ if (left == target) {
36+ left++;
37+ continue;
38+ }
39+ if (right == target) {
40+ right--;
41+ continue;
42+ }
43+
44+ long sum = arr[left] + arr[right];
45+
46+ if (sum == targetValue) {
47+ return true;
48+ } else if (sum < targetValue) {
49+ left++;
50+ } else {
51+ right--;
52+ }
53+ }
54+
55+ return false;
56+ }
57+ }
58+ ```
You can’t perform that action at this time.
0 commit comments