We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
functionclosestNumbers($arr){// Write your code heresort($arr);$count_arr=count($arr);$temp_angka1=[];$temp_angka2=[];$abs=[];$min=999999999;$hasil=[];for($i=0;$i<=$count_arr-2;$i++){if(($arr[$i+1]-$arr[$i]<$min)){$min=$arr[$i+1]-$arr[$i];unset($abs);unset($temp_angka1);unset($temp_angka2);$abs[$i]=$min;$temp_angka1[$i]=$arr[$i];$temp_angka2[$i+1]=$arr[$i+1];}elseif($arr[$i+1]-$arr[$i]==$min){$min=$arr[$i+1]-$arr[$i];$abs[$i]=$min;$temp_angka1[$i]=$arr[$i];$temp_angka2[$i+1]=$arr[$i+1];}}$count_abs=count($abs);foreach($absas$key=>$val){$hasil[]=$temp_angka1[$key];$hasil[]=$temp_angka2[$key+1];}return$hasil;}
Cookie support is required to access HackerRank
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 →
Here is my PHP solution: