CT42637

The Nearest Pair Sum Beneath Glowtide

MediumL5 · Recursion RenegadeAcceptance: 0.0%XP: 60

Nico found an old ledger of registers in the gallery attic at the lantern reef of Glowtide. 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 <= 1096; 0 <= a[i],k <= 1000.

Example 1:

Input: 398 530 767 518 739 285 663 653 861 431 45 806 978 377 52 721 686 573 910 384 362 487 395 987 924 716 447 818 456 202 848 138 924 621 783 125 945 181 976 607 998 16 756 399 541 247 91 772 255 326 918 307 885 61 747 218 550 704 140 851 51 254 512 146 820 228 954 54 709 209 601 111 385 95 287 652 640 98 923 489 500 355 228 977 336 96 989 244 867 185 525 165 632 275 938 659 955 512 132 194 873 64 137 2 378 229 898 861 80 430 425 979 51 655 71 951 603 228 641 943 421 791 866 322 984 122 830 492 414 653 617 581 790 440 877 32 926 366 44 448 198 451 450 631 391 293 642 351 527 492 24 38 791 291 22 312 381 440 137 288 190 87 671 81 822 745 560 858 802 367 857 907 738 943 699 602 119 338 803 694 625 534 256 735 983 40 616 762 882 180 46 473 184 327 33 971 783 717 725 809 14 812 58 511 848 287 295 …
Output: 530

Example 2:

Input: 118 578 768 767 910 552 692 395 244 831 125 979 785 460 546 116 907 896 510 656 835 895 981 908 384 686 221 647 747 127 608 49 107 14 494 359 681 381 967 435 886 430 939 584 606 69 388 570 386 295 285 115 769 230 471 238 787 657 396 57 102 226 761 604 326 992 311 681 105 589 39 990 430 542 789 318 912 318 958 340 530 298 659 235 615 22 744 485 809 432 818 727 282 708 354 79 61 288 553 413 942 1000 765 993 9 792 614 361 176 613 550 702 153 417 216 99 696 456 630 980
Output: 578

Example 3:

Input: 940 1975 79 521 470 494 322 208 157 926 611 731 143 6 216 166 174 201 206 858 987 508 173 794 783 797 412 799 441 829 573 638 103 571 176 761 679 659 502 317 418 445 123 558 282 77 671 950 451 863 56 147 363 644 949 394 711 944 166 247 369 409 871 315 298 847 626 40 156 565 335 426 600 454 508 99 904 325 854 106 199 164 472 916 725 791 502 724 482 164 166 228 49 51 600 832 9 206 201 654 122 293 64 10 372 146 23 629 674 183 141 248 53 561 197 388 34 613 625 87 721 134 635 717 101 506 325 709 646 830 812 635 881 763 509 100 899 462 934 479 630 322 763 468 622 908 205 287 507 312 600 84 736 681 985 75 180 226 481 836 912 357 281 430 312 7 40 644 957 686 625 786 552 248 534 355 864 3 871 272 776 827 79 470 928 42 730 89 427 442 380 914 823 163 94 988 573 383 547 672 586 802 598 501 574 165 913…
Output: 1975

Constraints:

2 <= n <= 1096; 0 <= a[i],k <= 1000.

Loading...
Test Cases:No test cases
No test cases available.