CT38768

The Timeworn Nearest Pair Sum

MediumL5 · Recursion RenegadeAcceptance: 0.0%XP: 60

Morning light hits the railway depot of Cinderline as Curator unrolls the day's manifest of crates. The tally will decide the next shipment. 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 <= 3131; -1000000 <= a[i],k <= 1000000000.

Example 1:

Input: 1840 1614788933 493848724 747244533 569489944 741950571 45925452 283188381 963137986 395559638 201960067 630622482 481914096 816104817 926465578 8918504 520239618 67225578 951546370 378538845 390224426 37574129 607832147 663620334 930446971 689081587 395542977 772428399 562090108 359176417 58465519 263591483 363565311 184280624 815978255 636478975 220129397 88862590 71051927 871410867 15583730 231423987 789793664 878626594 517146288 456896696 741418579 258135653 713011471 191307333 611662111 43581102 102789500 930784191 470045594 440639442 488920227 538406753 151160146 802985920 874791082 833104106 807818287 568885905 805889238 417763773 738911187 531906099 807734612 477741094 754188990 897989684 288024017 352295615 493168315 699970971 772448095 454104878 190616686 808372304 609559190 2924…
Output: 1614789632

Example 2:

Input: 2597 766022831 487049220 268254074 350920703 989001284 286846603 84456144 100763278 171141088 913215206 36972490 107214629 731791956 622672507 452246267 122576319 427427678 256750084 568808800 707717388 71911484 598317854 137085022 912558921 837627867 545942475 546393655 251718626 610807230 913823923 894504973 526478161 413991680 142942648 73021185 473373 606676770 913083589 523861154 469560313 225355007 257897762 398307687 559781281 520044186 536437752 18108897 100978292 640784709 260248161 146840736 544160245 373404073 788960143 764091333 8549083 756533680 744354372 431409957 635958658 317018572 763452945 273866558 669819807 57147880 720284824 636513015 542450569 98135787 6713208 627165632 300997408 140518592 907153095 783022758 359095870 362368939 433628952 900575900 86230774 284590393 …
Output: 766022886

Example 3:

Input: 1113 1912251712 950136682 480327682 278258691 969745447 700939553 994421455 971078385 634363972 376167470 811152657 649443397 645423105 870425887 610512870 467591720 251027421 397195211 401836045 689673985 440569412 847616891 183588606 916220183 536576260 259330580 699430938 649494895 388496284 492738572 584361862 207302045 313542676 174815631 362612359 661471798 680208192 886911342 109358094 250999654 389951952 209963516 749135150 45274046 487578205 820889556 983774634 865030926 214040784 659513603 620301128 331916126 360210081 995057641 950008188 671237195 841106509 898400134 419936448 850762165 885158862 477981858 96467547 448459646 676209039 879765239 382234401 920490667 651110246 356645173 446894695 698900399 847357118 172506417 269569372 198029938 740383745 505567801 223675192 492506…
Output: 1912256127

Constraints:

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

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