CT36975

The Nearest Pair Sum of Cascade Vale

MediumL5 · Recursion RenegadeAcceptance: 0.0%XP: 60

Deep in the ribbon falls of Cascade Vale, Qadir studies rows of crates entries. One clean number will settle the guild's oldest argument. 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 <= 3750; -1000000 <= a[i],k <= 1000.

Example 1:

Input: 2064 -801661 -650231 -991525 -309581 -126991 -578497 -721852 -915703 -548135 -25222 -10893 -258165 -509117 -380159 -459385 -391488 -527632 -538498 -756240 -936964 -466829 -711060 -229083 -73103 -296505 -164823 -833307 -112556 -412510 -524471 -512410 -208494 -215937 -170873 -259401 -670469 -660900 -536106 -336199 -746515 -849698 -919192 -520773 -643535 -738600 -53603 -201241 -115048 -645714 -802611 -487000 -255519 -371479 -434129 -782931 -997353 -12547 -725589 -485050 -136028 -633957 -922444 -361881 -477650 -188547 -946815 -275164 -825143 -16111 -990412 -464574 -235975 -506958 -87405 -816267 -833458 -37472 -13587 -964331 -736409 -57575 -449508 -746913 -527123 -436093 -667069 -115407 -233487 -645150 -109555 -276666 -592606 -940169 -835763 -649673 -616434 -462627 -696711 -643207 -594847 -9742…
Output: -801661

Example 2:

Input: 2471 -433144 -230317 -287897 -105642 -246331 -676293 -934428 -902889 -765106 -805205 -522670 -676719 -332587 -408673 -291555 -476946 -131388 -283477 -384001 -597154 -902378 -619770 -431294 -140340 -982045 -496522 -644013 -339170 -245042 -269467 -599023 -723913 -91977 -68910 -240656 -777595 -973096 -113956 -973887 -359255 -946279 -716871 -991886 -822552 -56435 -814608 -475610 -717694 -72446 -419018 -272212 -742281 -349277 -41189 -294069 -904762 -117476 -283261 -844638 -946403 -69756 -209714 -76635 -618872 -304466 -46577 -885249 -180616 -40382 -678332 -398768 -806816 -469240 -680551 -498766 -854966 -155931 -188337 -460758 -195583 -11472 -874260 -153727 -918950 -795511 -455327 -493840 -72109 -252907 -613839 -545678 -941663 -14678 -86501 -893680 -411225 -419865 -489296 -129653 -192626 -765606 …
Output: -433145

Example 3:

Input: 374 -57491 -678650 -146067 -486754 -874322 -535996 -192725 -924795 -397572 -771628 -113440 -645234 -977413 -137593 -450724 -483343 -583499 -327542 -663552 -272966 -523007 -264958 -567513 -328304 -27536 -501397 -279688 -836562 -664638 -946421 -982601 -363801 -111466 -147071 -831401 -449057 -562471 -875593 -377048 -116554 -637712 -933080 -716793 -741977 -627937 -49455 -756307 -455424 -659440 -346694 -438110 -954194 -952981 -945051 -994699 -269095 -576703 -526936 -135802 -834274 -204125 -583317 -182309 -683659 -578981 -787413 -397203 -943617 -750852 -292212 -873109 -802875 -894383 -951696 -561921 -559694 -162452 -401331 -378153 -640856 -887628 -776052 -131056 -668463 -538338 -148807 -521596 -333524 -997808 -999456 -498756 -495843 -265312 -706325 -850630 -406780 -354967 -464941 -138060 -144562…
Output: -57696

Constraints:

2 <= n <= 3750; -1000000 <= a[i],k <= 1000.

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