CT35823

The Serpentis Nearest Pair Sum

MediumL5 · Recursion RenegadeAcceptance: 0.0%XP: 60

Legend says the vault under the jade shrine of Serpentis opens only for a perfect tally of weights. Gardener 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 <= 2977; -1000000000 <= a[i],k <= 1000.

Example 1:

Input: 1204 -984472342 -613446407 -581457024 -796713540 -137779389 -607712601 -589930734 -864265837 -796681249 -986976680 -309014698 -844248022 -967705869 -316044458 -345360619 -49630803 -249764897 -326585659 -251714036 -225780796 -513979138 -511979719 -669426619 -522276912 -256867790 -429785901 -202059342 -472226165 -435326230 -858499714 -288403518 -551486096 -589766081 -389068699 -11187775 -880276908 -959403713 -382541764 -818233390 -875610586 -883890701 -796135182 -292735943 -625771191 -130605867 -630682558 -477819757 -433127450 -647045759 -357760383 -165035496 -996266293 -952884068 -834721449 -20974822 -909152429 -881979427 -969866 -936494883 -56400236 -570022526 -647305737 -763967776 -740093155 -342109232 -739675145 -715354799 -530706169 -941523932 -643475222 -284662802 -646928487 -650682772…
Output: -984472186

Example 2:

Input: 878 -421670635 -39279111 -427786951 -386699092 -725605444 -196847233 -877332583 -284421545 -987701106 -244944016 -807948290 -625776625 -902801173 -942050899 -986395122 -799225412 -995357518 -989923928 -796085804 -602477865 -503767095 -281479619 -775209303 -188928417 -955628279 -421966246 -808212161 -332050076 -862052758 -697590811 -388736976 -139239181 -332959283 -845547862 -489872418 -555105656 -406687172 -699150175 -158994514 -733741539 -516123315 -411159424 -48056828 -183905855 -821978249 -657536121 -429839298 -458642040 -709200 -581319710 -901699818 -667496471 -900789185 -848928972 -136583164 -430531761 -769713804 -433940799 -523581440 -452444717 -879439190 -231784540 -295694716 -797769298 -447839994 -742033793 -594701696 -330898697 -192421406 -882441129 -843672214 -228891995 -43974986…
Output: -421675329

Example 3:

Input: 2377 -81798473 -665756216 -622118639 -88920601 -975969301 -339946339 -14057535 -867039462 -696325556 -374908109 -214452899 -679041054 -626385660 -89127198 -775669795 -482725918 -752247091 -196269191 -279715392 -356410701 -572132937 -413706776 -413937451 -577357613 -103068691 -816311548 -190751077 -556025421 -875093454 -198215663 -484492007 -563133416 -861348161 -852311829 -563977698 -86914595 -614729149 -959640388 -575232828 -365103882 -862527356 -294096552 -832096814 -472702363 -19750971 -153682603 -102492080 -744455825 -793550744 -774864425 -818869444 -447516954 -297748482 -615915706 -384362551 -526802250 -764460924 -894154712 -352063558 -362945973 -544758721 -521548290 -78048296 -289017930 -908578744 -920548927 -564910217 -937681540 -600646281 -736493461 -389131702 -786485776 -354093027…
Output: -81797561

Constraints:

2 <= n <= 2977; -1000000000 <= a[i],k <= 1000.

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