CT43618

The Mirrormere Nearest Pair Sum

MediumL5 · Recursion RenegadeAcceptance: 0.0%XP: 60

The clerk Mason has been asked to decode the foreman's shorthand notes. On the desk lies a long table of crates readings. 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 <= 3606; -1000000 <= a[i],k <= 1000000.

Example 1:

Input: 317 -181425 -488604 969998 414960 867090 551565 -849490 265256 406700 -381152 87414 875649 164714 -890385 308375 356892 191907 -399779 780028 -294058 -307373 550169 890276 -279929 -134423 656827 -562951 579528 303763 -154204 -820965 398958 -486279 -459442 -588892 -379018 119678 -473790 -180626 638103 264372 -220446 421368 -22986 422583 -677755 98778 -99016 -136612 195556 237322 -439761 587539 -40792 965892 128825 622897 776831 -838239 357635 698852 893395 99947 139732 775598 -312322 836314 -472813 386368 452884 556315 929752 -256057 -312014 812333 173643 240206 461501 842017 -194181 -896892 -912845 410330 515250 715392 798988 461602 -997297 -391362 -471495 407188 -125923 -107801 -335239 -917320 874313 847637 -317845 -685787 -169165 965774 980504 -40704 731283 -478079 640675 -700408 -542703…
Output: -181412

Example 2:

Input: 2137 460856 -289915 -204151 911768 176074 837626 617107 -367091 -393306 -354386 346691 719321 185238 195407 106981 -501606 -586415 -505820 -626796 262746 -696064 158885 708319 809022 -627217 165459 -417845 569895 589988 542726 -532100 391305 25794 858603 580989 -290353 220867 700537 -467941 540309 -574440 493770 -93082 -207338 864889 111017 934546 -382139 -645781 -467601 -716283 -825151 967933 -240811 -442302 418783 -537045 -552848 -80332 -472977 -520810 -591205 -957231 145776 -125773 -37277 -923471 139611 372323 849061 475601 927471 -974705 -380007 254810 -635478 -970578 -825051 -615081 473445 520970 -86254 -670249 -684085 653150 -943336 -486461 517740 651178 -98243 47223 490813 -722078 744103 835449 -652232 112808 -718701 422194 -974861 -849098 694205 -514226 563167 965161 -895328 913668…
Output: 460856

Example 3:

Input: 2669 354625 -427048 815762 -805762 244437 -403480 599257 -268231 85248 977758 857748 182295 534035 -178727 714953 93714 -37499 -796800 -363338 -457378 842397 -649833 668168 894254 -723241 -831387 -23024 -115066 882655 -644418 -199916 962181 917550 -564202 -304682 861611 536224 968194 -635287 -298752 -373473 579477 562478 -126565 -917078 -127170 814686 532969 904106 -509824 575489 -292853 832148 23926 -56509 -308662 28728 718607 -675593 363362 620037 488708 986624 315733 831738 500123 -47195 475423 964127 586709 666479 -612154 -11994 -950002 227958 -636507 689328 -52625 595695 41777 886936 -761857 899780 -822826 -83470 740040 877411 -919424 634086 -11147 -76776 -84055 45828 -208336 -703990 -888219 -897169 77325 -445492 439946 -234955 -26367 897837 580125 798547 688447 -863869 285649 -751390…
Output: 354625

Constraints:

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

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