CT43998

The Grapehollow Nearest Pair Sum

MediumL5 · Recursion RenegadeAcceptance: 0.0%XP: 60

Galen found an old ledger of shelves in the counting house attic at the terraced vines of Grapehollow. The margin notes pose a precise question. 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 <= 3393; 0 <= a[i],k <= 1000000.

Example 1:

Input: 1034 1979991 402034 476845 303242 978065 839470 376774 291213 268580 211431 648288 64397 289794 474952 716483 73848 768663 671445 602192 729260 946829 107126 436454 135193 937617 185169 726947 223413 333715 41715 496045 813143 480394 686265 671488 380719 737649 815898 101118 935972 881899 45895 625096 689820 180536 867861 812699 828936 483503 218860 194000 34367 565317 879005 845676 463710 748582 805286 754725 979929 718207 168910 254172 79864 410376 424463 461790 970548 464507 822812 160584 484362 379736 395978 579292 938842 975476 615662 725331 680323 727269 34840 885004 561911 60034 879237 992838 349899 185409 533383 896633 177078 89149 44668 860494 892648 989865 86925 467651 992011 432259 181894 668958 763640 55751 341635 548108 361206 245927 390588 453188 334543 109669 563476 519523 9…
Output: 1980007

Example 2:

Input: 1696 509767 653977 489700 105028 374654 578640 459901 869711 415534 513452 575881 30954 630076 524784 444661 800182 873950 618236 658341 371745 939260 656584 229483 976552 465006 926511 511938 32244 767105 260663 986695 958360 136152 13416 343992 519117 173695 949731 163984 962979 556574 766894 76935 614447 996116 987670 353796 874536 151580 957015 635216 851056 296665 950749 476478 452215 625378 628656 826457 774528 693711 681339 930990 682287 434838 469483 471371 77664 676976 510299 121856 986555 409255 334868 222649 110100 156998 2720 839577 984707 467444 299009 326855 838310 532736 981919 346408 806208 761299 240038 801154 428213 147918 208318 381993 616987 662364 685257 537635 172171 410195 817924 468528 228078 802139 599138 190353 835019 404579 880072 691303 960401 935554 277018 4400…
Output: 509766

Example 3:

Input: 2947 790714 152483 381757 628009 943491 133200 929240 28252 14369 338585 889315 793445 951004 888884 184877 108202 30260 270742 310860 676710 282430 943084 560831 856737 201547 311636 924402 791493 149077 35718 62290 277513 369113 203155 694757 364973 279269 699658 37881 516808 131508 334746 148295 371973 452407 676022 112003 742065 682952 322485 819628 608857 67428 81486 67788 177935 236221 942619 113324 125829 575282 759311 208543 35053 246455 208549 61527 905118 728921 476349 542504 848527 554588 527606 70371 45680 650624 698140 534176 801559 913166 880054 133495 777909 128604 350300 643859 506207 392764 614901 322301 34335 479443 754709 662523 421620 896208 175300 492364 793033 590057 583610 813820 625677 171504 954738 491068 693701 454754 710966 872977 557854 508342 116943 313275 5705…
Output: 790714

Constraints:

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

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