CT47394

The Nearest Pair Sum of the Brass registers

MediumL5 · Recursion RenegadeAcceptance: 0.0%XP: 60

In the herring shoals of Silverfin, June the log keeper keeps careful count of the registers. Schedule the next expedition, 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 <= 3106; 0 <= a[i],k <= 1000.

Example 1:

Input: 2131 393 16 272 353 867 249 58 162 997 20 194 805 20 228 241 495 96 201 883 173 933 386 796 986 67 829 415 567 411 271 787 533 953 99 983 19 419 120 203 7 798 624 516 320 826 89 934 335 436 519 645 593 488 413 315 659 54 211 17 932 84 872 190 756 954 299 666 578 135 113 707 445 767 27 873 615 334 955 225 859 302 179 603 548 905 762 568 408 434 763 248 670 325 34 990 964 170 545 850 795 536 600 150 411 500 949 26 104 819 699 808 934 667 583 310 521 216 986 484 78 406 111 529 950 79 129 493 126 495 552 125 103 481 322 395 43 177 637 486 965 29 471 652 313 525 211 123 236 528 410 886 871 422 76 985 628 965 633 595 43 868 681 992 851 91 833 809 509 549 730 883 817 272 28 553 552 246 208 31 434 257 185 472 921 245 655 500 741 809 65 862 91 58 325 959 183 852 749 509 189 133 713 619 248 105 842 …
Output: 393

Example 2:

Input: 262 384 539 663 373 356 281 381 350 691 464 604 569 704 791 742 139 404 271 139 369 259 332 803 140 297 41 90 205 799 433 747 934 458 108 956 178 617 466 707 846 252 654 427 414 586 145 222 167 47 397 335 16 650 400 1 160 951 50 827 886 873 779 48 885 383 528 131 587 571 351 812 856 895 109 414 846 299 738 721 879 986 431 46 855 817 955 579 677 136 824 384 866 720 39 95 176 322 601 767 767 781 105 43 939 439 87 780 526 816 924 789 209 604 432 318 613 352 272 906 139 588 120 132 248 83 256 634 537 316 406 104 341 553 205 245 823 131 59 604 181 180 133 420 283 771 256 990 121 992 491 156 233 902 849 645 663 855 465 668 9 40 993 66 78 350 677 206 446 778 993 851 890 493 681 256 950 379 925 957 990 386 103 694 22 266 971 762 257 30 625 210 915 479 745 447 29 863 799 980 755 800 552 274 253 207…
Output: 384

Example 3:

Input: 1422 1011 237 58 117 771 584 789 928 178 914 93 398 726 28 984 840 948 789 741 890 643 846 653 180 952 850 711 670 250 464 157 609 541 23 842 656 711 639 651 995 255 845 237 543 267 282 985 405 337 835 656 909 90 518 881 448 814 273 559 594 524 691 60 729 839 683 158 909 364 346 273 749 770 825 656 688 54 573 848 979 603 788 819 309 59 621 514 545 953 396 17 48 989 556 810 272 517 8 105 274 643 246 680 510 897 265 719 787 628 75 751 618 427 159 408 172 310 531 630 121 900 378 191 379 216 255 832 858 541 87 559 499 382 831 311 739 270 915 630 242 904 928 950 62 899 158 220 224 317 867 637 287 500 207 435 172 829 159 89 420 783 284 961 156 881 234 988 749 1000 281 843 610 375 393 438 760 583 253 134 171 731 47 786 711 186 907 943 440 190 560 212 640 907 449 779 46 705 848 608 560 319 570 84 …
Output: 1011

Constraints:

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

Loading...
Test Cases:No test cases
No test cases available.
The Nearest Pair Sum of the Brass registers - MEDIUM Coding Problem | CodeTikki