CT42708

The Hidden Nearest Pair Sum

MediumL5 · Recursion RenegadeAcceptance: 0.0%XP: 60

In the forest village of Briarthorn, Clerk the assayer keeps careful count of the registers. Prepare the guildmaster's brief, every figure matters. 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 <= 3114; 0 <= a[i],k <= 1000000.

Example 1:

Input: 195 1563711 440596 28481 183298 43304 66735 483647 972902 170481 604754 370354 264651 592489 413596 144275 769563 922230 230228 90023 487993 148469 454518 374346 789216 176125 536133 129944 903065 740556 355947 297546 15047 757800 983492 394797 838791 310659 637706 958583 241865 224800 82516 716696 44023 43220 403541 798855 521812 81364 633182 413184 786065 850342 498434 901017 936744 413419 879270 16580 653045 938586 839362 33629 233783 605113 472337 289393 111808 88504 364185 964985 518442 301184 57357 230392 537883 421865 65645 735934 36953 96296 482487 36336 668770 798786 486242 944669 846720 151146 269566 328551 671396 612755 332004 878590 608548 9426 683806 400687 479743 608032 420167 87493 11896 188189 528188 197677 886177 532151 736878 263011 889142 200835 806692 569023 325407 2709…
Output: 1563696

Example 2:

Input: 1157 1187111 398192 356939 28865 218435 752925 535292 77928 988870 434540 358650 277332 944522 265061 782551 34463 196993 237112 862569 858301 384504 716980 688440 401385 135366 658178 747887 281201 871203 861921 892602 153113 722811 539502 406395 909236 807156 439724 996582 785457 866005 516046 721067 585794 117393 7813 797493 749408 837374 243875 576174 467957 843476 255605 973148 760605 36959 232338 665495 698184 83652 741948 701236 235610 727092 717492 239641 380509 329287 643913 145499 959114 937426 469654 286187 21659 94907 853772 315808 290145 215313 299411 908991 331569 173290 460569 240330 822808 319741 189563 415021 242176 906700 283712 456394 696331 37629 887308 19157 905268 936702 161753 999035 161635 5575 362997 44018 114401 899709 468714 48225 821137 201315 475539 924270 9511…
Output: 1187112

Example 3:

Input: 875 1151826 922835 306398 946814 446246 711471 545235 674424 582505 113907 294238 572 16753 447601 981096 890079 731154 287165 786549 22120 428682 134152 182105 426180 474108 780762 595317 22230 393977 200673 236196 604587 734759 625747 196118 761761 564337 273909 25652 98426 247849 2921 26488 814168 219378 727610 334452 237232 746165 420646 231431 463118 138183 812628 882693 115388 627914 180176 365168 197828 625980 107070 387077 596714 508818 637772 853809 840071 701989 34508 938400 901070 228288 654768 482442 804943 836435 405708 550720 463767 544726 387549 291774 122789 870183 878642 388659 576872 807576 151403 19454 135435 296619 598766 76723 663964 458242 585122 149600 671818 919505 807239 21725 379199 967428 906600 732897 811218 844091 530156 15861 31008 501757 795855 551637 144100 …
Output: 1151825

Constraints:

2 <= n <= 3114; 0 <= a[i],k <= 1000000.

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