You are viewing a single comment's thread. Return to all comments →
PHP solution
sort($arr); $minDiff=$arr[1]-$arr[0]; $res=[]; for ($i=0; $i < $n-1; $i++) { if($arr[$i+1]-$arr[$i] < $minDiff ){ $res=[]; $res[]=$arr[$i]; $res[]=$arr[$i+1]; $minDiff=abs($arr[$i+1]-$arr[$i]); }elseif($arr[$i+1]-$arr[$i] == $minDiff ){ $res[]=$arr[$i]; $res[]=$arr[$i+1]; } } for ($i=0; $i < count($res); $i++) { echo $res[$i].' '; }
Seems like cookies are disabled on this browser, please enable them to open this website
Closest Numbers
You are viewing a single comment's thread. Return to all comments →
PHP solution