File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ const input = require ( "fs" )
2+ . readFileSync ( process . platform === "linux" ? "/dev/stdin" : "./input.txt" )
3+ . toString ( )
4+ . trim ( )
5+ . split ( "\n" ) ;
6+
7+ const answer = [ ] ;
8+ const arr = [ ] ;
9+
10+ for ( let i = 2 ; i < input . length ; i += 2 ) {
11+ arr . push ( input [ i ] . split ( " " ) . map ( Number ) ) ;
12+ }
13+
14+ for ( let i = 0 ; i < arr . length ; i ++ ) {
15+ const tmp = arr [ i ] . sort ( ( a , b ) => a - b ) ;
16+ const n = tmp . length ;
17+ let count = 0 ;
18+
19+ for ( let j = 1 ; j < n - 1 ; j ++ ) {
20+ const mid = tmp [ j ] ;
21+ let left = j - 1 ;
22+ let right = j + 1 ;
23+
24+ while ( left >= 0 && right < n ) {
25+ const leftDiff = mid - tmp [ left ] ;
26+ const rightDiff = tmp [ right ] - mid ;
27+
28+ if ( leftDiff === rightDiff ) {
29+ count ++ ;
30+ left -- ;
31+ right ++ ;
32+ } else if ( leftDiff < rightDiff ) {
33+ left -- ;
34+ } else {
35+ right ++ ;
36+ }
37+ }
38+ }
39+ answer . push ( count ) ;
40+ }
41+
42+ console . log ( answer . join ( "\n" ) ) ;
You can’t perform that action at this time.
0 commit comments