CT56378

The Gusthaven Nearest Pair Sum

MediumL5 · Recursion RenegadeAcceptance: 0.0%XP: 60

It is audit day at the windmill fields of Gusthaven. Zoravar spreads the ledgers records across the table and picks up a sharp pencil. 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 <= 1591; -1000000 <= a[i],k <= 1000000.

Example 1:

Input: 761 408829 -263485 -224259 823545 -917271 654194 903775 991549 852350 269445 109777 683163 -482956 -784683 227289 -318325 101850 -292227 -926514 402370 984738 -176032 -626980 -604951 640082 760523 485497 465732 -780846 248664 264470 730023 630064 -607957 -477156 368637 818777 -305932 293940 670352 696317 -586828 169189 -379863 647839 707417 -789681 -480081 298400 -845060 567002 629845 298779 -546081 -536120 83811 -577691 794432 -792489 -844397 -106297 797717 194960 -727268 -103998 -469560 773718 523392 -558398 -100910 283450 896739 683826 583225 716617 404544 625853 -357621 -652423 -440412 -578480 -898007 617669 -429515 -468286 391450 -252548 784203 179669 930085 267195 682053 -670575 515107 -594040 599539 258581 -523713 -994148 -259954 -253655 937188 120782 -139966 375141 -43187 27505 -48…
Output: 408831

Example 2:

Input: 1401 -615069 -184387 110117 -216321 -313298 406307 -270118 -208631 428316 -270142 -265845 -528213 333970 -988771 732716 151130 -121403 456606 -865491 -390650 -844332 -321022 -353937 -265425 -626922 -556039 953469 785204 157307 152210 32945 -877895 736923 -89779 -734973 175623 922027 942244 742449 863966 738768 35817 -945093 -661270 109331 -730209 530998 -552704 -422058 -447437 -860648 -943115 -676777 775296 -886406 -198292 -940877 609728 -558134 181108 375695 63864 795690 -129160 610756 605953 -803009 595842 995017 543453 18570 -211815 -412199 -904812 453563 -886953 613393 -587013 -268177 -509175 -166385 -68510 872379 -31338 763492 -619770 -778606 -837116 292362 -820815 416768 651776 -937891 209392 -884186 -999946 -277212 843913 537501 681041 -953789 25302 -121535 -244070 960134 610836 -62…
Output: -615069

Example 3:

Input: 65 1557798 617630 161238 -143883 -88300 -388806 729611 -306586 -509865 -191418 349004 819658 -295651 -604247 -145275 -333159 735415 632370 -266597 694394 968021 -973071 -227624 255565 -199893 -239686 733780 -514402 -818481 930721 -98282 -542077 -876557 42744 -443286 671667 397882 138724 166629 951461 773305 -673478 -291941 366356 -944248 221328 -434998 43475 -522824 191452 392274 -763665 -502889 31009 263651 -869084 -631313 -573957 -575245 -419334 -179349 399667 -27602 -650190 419376 665085
Output: 1555073

Constraints:

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

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