File tree Expand file tree Collapse file tree 1 file changed +55
-0
lines changed
Expand file tree Collapse file tree 1 file changed +55
-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+ class boj9024 {
6+ static BufferedReader br = new BufferedReader (new InputStreamReader (System . in));
7+ static StringTokenizer st;
8+ static void nextLine () throws Exception {st = new StringTokenizer (br .readLine ());}
9+ static int nextInt() {return Integer . parseInt(st. nextToken());}
10+
11+ public static void main(String [] args) throws Exception {
12+ nextLine();
13+ int t = nextInt();
14+ while (t-- > 0 ) {
15+ nextLine();
16+ int n = nextInt();
17+ int k = nextInt();
18+
19+ int [] arr = new int [n];
20+ nextLine();
21+ for (int i = 0 ; i < n; i++ ) {
22+ arr[i] = nextInt();
23+ }
24+
25+ Arrays . sort(arr);
26+ int left = 0 ;
27+ int right = arr. length - 1 ;
28+ int best = (int )(2 * 10e8 );
29+ int count = 1 ;
30+
31+ while (true ) {
32+ int sum = arr[left] + arr[right];
33+
34+ if (Math . abs(sum - k) == best) {
35+ count++ ;
36+ } else if (Math . abs(sum - k) < best) {
37+ count = 1 ;
38+ best = Math . abs(sum - k);
39+ }
40+
41+ if (sum == k) {
42+ left++ ;
43+ right-- ;
44+ } else if (sum < k) left++ ;
45+ else right-- ;
46+
47+ if (left >= right) {
48+ break ;
49+ }
50+ }
51+ System . out. println(count);
52+ }
53+ }
54+ }
55+ ```
You can’t perform that action at this time.
0 commit comments