CT45945

The Nearest Pair Sum of the Quiet shelves

MediumL5 · Recursion RenegadeAcceptance: 0.0%XP: 60

Every season at the beacon hill of Lantern Tor, someone must restore an old family record. This year the task falls to Lara, armed only with the shelves register. 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 <= 2553; 0 <= a[i],k <= 1000000.

Example 1:

Input: 1727 556300 296589 578272 164770 635136 264544 410204 580537 722831 311956 455459 300369 796733 98351 601439 250533 795652 14431 890775 118602 938528 644738 768051 964307 537556 793244 136107 733921 331718 430236 855732 923757 212286 656273 735568 833499 676032 824138 214008 391837 799532 474670 543163 734358 769618 635871 543848 218332 378794 207757 785149 791846 387976 211230 216054 7547 455012 333622 169048 984845 130382 594752 233498 22485 527915 606658 621479 284834 525464 668735 305750 319525 480026 221706 98526 68090 574434 191070 387737 308793 419330 199835 466817 730847 933410 887309 124545 795209 363758 84991 314674 145297 410456 710502 202657 604070 275559 952354 842139 937683 332723 396055 811277 711685 569024 843950 613462 348242 343399 106334 766065 797703 181008 670107 58141…
Output: 556301

Example 2:

Input: 1055 994985 591993 937363 245000 307283 377040 214885 761471 878705 92537 752601 962897 828429 123102 600650 583600 925281 818052 744156 244324 457329 943975 670926 60268 545542 619027 390823 312214 811134 842660 95523 908255 524672 365856 431511 480755 938281 305122 695351 907401 851277 937157 166555 419704 969982 244744 907951 671754 660468 734126 742747 312179 517322 927833 947511 471283 662126 868171 964838 4589 386439 249754 144397 891980 367399 863204 938975 809979 910311 118734 39048 196195 88136 1247 831359 910669 296918 878501 997352 746579 557658 551478 605094 717627 258089 579750 728504 897806 92916 111040 293298 482540 577240 503315 513461 693339 107012 988370 413573 988643 678916 405976 803837 231698 483375 243256 104931 111855 263774 167851 227780 844179 29546 373275 674009 9…
Output: 994984

Example 3:

Input: 1521 1796277 411032 338606 251324 122436 890300 349866 260091 153048 573646 188863 362568 241686 985163 106031 46093 964967 976113 853750 960768 451781 845316 36046 272082 877656 948261 426599 15873 463198 689735 330881 991873 493662 302508 356477 785406 893642 934808 820587 959835 67358 13271 317657 760327 436421 562424 521081 195855 329480 940370 170413 111268 811858 646138 731491 512900 413147 898208 695225 172112 827468 935158 221055 201353 112290 520589 943469 770993 373104 892071 968357 562625 109149 784000 291431 844128 103979 352736 389489 788739 704700 580943 822811 218070 260797 679474 505577 342156 788366 916310 380236 667112 65727 491971 149577 987570 797255 130542 653343 935109 513594 864351 688187 861548 484101 987679 448720 757393 93002 545339 321731 813499 908591 925286 370…
Output: 1796270

Constraints:

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

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