CT53357

Copper shelves Nearest Pair Sum

MediumL5 · Recursion RenegadeAcceptance: 0.0%XP: 60

Legend says the vault under the herring shoals of Silverfin opens only for a perfect tally of shelves. Amara means to find out. 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 <= 2754; 0 <= a[i],k <= 1000.

Example 1:

Input: 3 1238 559 149 406
Output: 965

Example 2:

Input: 1658 1881 677 936 29 102 55 433 93 413 1 720 446 179 621 68 81 777 246 582 43 426 283 722 727 256 568 302 827 914 73 882 29 298 993 454 636 581 249 433 216 497 704 997 214 643 992 290 393 333 215 34 465 357 49 83 120 244 450 129 820 153 765 154 765 635 53 10 249 426 144 506 370 0 757 213 183 582 663 211 302 515 727 275 21 792 29 622 211 81 806 436 970 847 131 237 90 623 539 559 12 804 384 758 108 900 258 249 941 774 161 929 270 251 956 557 56 164 653 535 617 552 152 332 904 298 122 124 485 738 363 403 640 825 803 970 618 148 471 506 471 400 867 798 659 945 906 756 369 276 940 859 577 308 19 928 979 619 45 220 115 82 362 268 617 686 479 222 367 287 17 282 527 135 824 329 562 248 52 549 936 806 536 178 965 898 666 596 274 907 472 200 441 94 825 286 327 607 589 809 882 508 985 883 259 277 582…
Output: 1881

Example 3:

Input: 839 1221 264 819 450 878 664 217 827 864 206 806 347 944 44 895 787 464 781 25 305 992 131 862 884 808 372 801 321 259 529 660 49 6 872 89 942 26 324 841 144 758 822 889 53 810 952 88 10 856 519 558 849 339 395 996 512 839 491 263 483 521 666 2 374 63 860 352 645 16 264 99 971 723 12 964 736 758 259 721 165 855 633 134 368 408 116 427 651 188 287 428 442 851 667 765 719 331 1 181 341 804 289 793 427 253 0 343 991 833 824 615 676 370 10 833 653 248 258 962 759 248 74 927 392 23 388 117 513 952 843 968 869 679 113 655 477 930 253 503 855 668 401 666 934 758 466 929 88 708 998 912 296 761 589 92 628 53 302 164 824 735 633 140 323 686 723 697 847 871 266 333 741 4 913 432 958 668 819 734 587 282 907 36 399 627 483 942 242 331 743 609 412 903 118 403 479 600 853 441 379 950 275 352 698 940 714 …
Output: 1221

Constraints:

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

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