CT45837

Kabir's shelves Nearest Pair Sum

MediumL5 · Recursion RenegadeAcceptance: 0.0%XP: 60

The log keeper Kabir has been asked to outscore the rival chapter. On the desk lies a long table of shelves readings. 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 <= 2299; -1000000 <= a[i],k <= 1000.

Example 1:

Input: 183 -974497 -145233 -777957 -938064 -411341 -836499 -463838 -978794 -366943 -608 -236679 -257400 -672360 -821558 -175648 -821803 -706400 -474275 -961260 -225121 -151381 -441694 -632648 -84437 -140466 -333378 -168075 -296646 -591426 -299611 -770880 -25784 -568270 -179485 -638096 -976225 -166413 -285737 -182584 -551044 -546984 -561472 -870926 -527150 -694635 -634694 -720859 -519467 -284413 -955687 -645133 -384494 -638467 -906809 -261381 -868007 -873907 -928898 -992356 -826571 -970 -289055 -16983 -579404 -675979 -101906 -504663 -687957 -827040 -806082 -400187 -506261 -147742 -87885 -411441 -961490 -423440 -981717 -346437 -505084 -267568 -234450 -583048 -757874 -827366 -357303 -886802 -808418 -918870 -801404 -432666 -738112 -750182 -764049 -305711 -534555 -739244 -724848 -418308 -600684 -81739…
Output: -974504

Example 2:

Input: 1227 -675953 -28403 -673703 -318120 -495282 -607726 -479669 -811854 -661093 -928184 -839296 -515311 -25070 -708325 -377118 -702553 -356223 -364051 -990822 -589185 -297671 -981334 -964775 -126410 -211560 -540411 -596578 -693585 -661293 -447016 -209690 -600997 -384046 -660646 -549178 -370200 -950863 -19723 -256880 -29340 -752258 -572094 -549715 -47237 -48699 -445324 -566958 -916046 -477912 -194633 -449228 -626510 -705131 -994302 -85979 -457891 -424873 -928719 -788824 -213588 -539127 -712340 -47296 -61264 -573109 -361528 -953139 -488508 -335672 -709583 -156812 -501043 -872840 -824623 -771836 -217080 -299460 -782779 -79917 -47575 -923822 -67048 -840528 -642815 -261959 -663365 -416261 -471707 -915153 -886834 -26147 -477196 -396850 -618820 -220066 -939194 -888325 -706189 -666390 -150456 -261833 …
Output: -675948

Example 3:

Input: 1810 -245062 -427351 -494251 -672056 -521941 -711045 -479074 -72297 -197235 -970949 -190939 -343833 -553118 -273560 -105287 -158670 -390660 -526468 -337171 -750901 -688350 -616354 -282929 -837740 -56321 -935750 -429157 -311251 -428425 -55291 -996774 -717054 -64845 -930067 -58285 -492433 -843410 -963832 -520625 -541695 -516614 -504451 -455413 -350311 -61770 -516136 -402984 -374131 -3448 -522087 -273206 -132941 -737970 -246520 -790367 -871269 -522251 -638360 -439145 -818750 -2024 -30979 -200043 -79473 -67618 -287702 -376015 -943025 -429409 -750162 -701142 -933603 -239926 -197851 -759165 -592754 -171280 -253801 -234050 -40501 -280423 -639179 -909024 -797620 -570777 -853455 -968820 -861114 -512902 -175446 -910936 -973909 -538963 -335252 -468925 -994358 -158634 -755850 -174804 -739555 -395708 -…
Output: -245061

Constraints:

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

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