CT35613

The Gearford Nearest Pair Sum

MediumL5 · Recursion RenegadeAcceptance: 0.0%XP: 60

Legend says the vault under the clockwork quarter of Gearford opens only for a perfect tally of shelves. Freya means to find out. Task: Among all pairs of distinct elements, print the pair sum that is closest to k. If several tie, print the smaller sum. Input format: First line: n and k. Second line: n integers. Output format: Print the closest achievable pair sum. Constraints: 2 <= n <= 3192; -1000000 <= a[i],k <= 1000000.

Example 1:

Input: 1519 860010 -71729 -246053 -145034 -877293 -567926 -428159 47425 781851 -284388 582015 309873 -755711 409470 -740435 -172962 -880241 21759 103162 847341 751287 698669 -116432 687822 586778 994026 -129595 -957282 164924 449311 -682160 -291917 479104 -595969 626392 395506 885384 718328 972591 596017 -148571 442720 -919274 -449426 729305 -184235 197172 343092 66439 -713526 310200 663748 691858 164291 897193 129620 433332 228775 -142046 45950 -222378 278180 248558 -550254 192334 -997197 -128997 611759 177344 -224620 -94722 775289 -203623 -681342 538895 880530 453274 -998091 820805 -672392 681446 -245628 -495586 -306345 61668 -124660 -522267 -594639 330521 768560 700215 120475 638093 177556 -558970 829252 -863007 328595 -347247 -538919 -693491 886214 187907 -308791 -109736 -809723 -941892 -9028…
Output: 860007

Example 2:

Input: 3070 -697941 -987473 -517146 -877768 610364 -638248 -603489 -343296 493700 -501315 -523216 305023 -618775 -27767 478544 924272 788253 696066 -198557 79574 -221957 144248 658762 -203512 776507 679004 -630340 -667339 -501904 293691 120132 -780203 -710155 -428437 823838 -354562 -885918 -38858 798570 627736 -926704 348925 -853662 873171 479526 -427913 105330 228064 772833 427319 396114 324453 -949890 -159006 -417147 677329 125688 -664327 -164822 449776 478720 -189936 -176372 -340792 828780 -878428 607386 -555443 -786922 -895057 -981921 -575359 305745 678096 714428 -457467 -189258 -16097 -68579 -322970 -191993 -301683 -389616 504812 532954 187604 -448723 -338271 -693003 727227 -617722 -544170 -872239 -922830 -237377 -963308 -773339 375214 -881197 -968027 126323 398232 -40485 -106126 149668 1552…
Output: -697941

Example 3:

Input: 2951 1629686 81733 891105 -104650 -741382 -729463 -181583 533092 -628571 -204108 916947 518534 -694583 690276 -612863 -381567 -532363 -354856 -452385 212369 -13333 -150018 -412631 394922 28882 355539 -229869 220655 617112 -974020 122320 -672938 -412242 46627 169588 -149291 221513 136080 748056 -376584 989654 -454308 135552 -961091 -823136 114621 -464404 -878105 -855029 -553984 70278 -708520 856305 -885032 773735 699359 -855153 -864579 941308 -694895 665659 46024 457728 -275532 410917 502071 -913376 -580022 -277043 -887773 -554112 760026 699221 -103995 -59602 859731 130896 -160106 -722741 371753 -717586 -788277 837241 -747194 904285 -755005 935648 341568 373035 397815 -878990 -94477 203874 423896 258438 -132394 447359 -632799 487436 538484 -747686 234736 -790207 971272 597115 -321029 83948 …
Output: 1629686

Constraints:

2 <= n <= 3192; -1000000 <= a[i],k <= 1000000.

Loading...
Test Cases:No test cases
No test cases available.
The Gearford Nearest Pair Sum - MEDIUM Coding Problem | CodeTikki