https://app.codility.com/cert/view/certSZ2UB8-GD363VTMK4H6FNPS/details/

 

There are N sheep relaxing in a field. They are positioned at points with integer coordinates. Each sheep has a square sunshade, so as not to overheat. The sunshade of a sheep standing at position (X, Y) spreads out to a distance of D from (X, Y), covering a square whose middle is at (X, Y) and whose sides are of length 2D. More precisely, it covers a square with vertices in points (X − D, Y − D), (X − D, Y + D), (X + D, Y − D) and (X + D, Y + D). Sheep are in the centres of their sunshades. Sunshades edges are parallel to the coordinate axes.

Every sheep spreads out its sunshade to the same width. No two sunshades can overlap, but their borders can touch.

What is the maximum integer width D to which the sheep can spread out their sunshades?

Write a function:

function solution($X, $Y);

that, given two arrays X and Y of length N denoting the positions of the sheep, returns the maximum value of D to which the sheep can spread out their sunshades. There are at least two sheep in the flock, and no two sheep share a point with the same coordinates.

 

 

 


function quick_sort($D, $s, $e)
{ if($s == $e) return array($D[$s]); $m=(int)(($e-$s)/2); $D1=quick_sort($D, $s, $s+$m); $D2=quick_sort($D, $s+$m+1, $e); $ret=array(); $l1=0; $l2=0; while($l1 < sizeof($D1) && $l2<sizeof($D2)) { if($D1[$l1][1] <= $D2[$l2][1]) { $ret[]=$D1[$l1]; $l1++; continue; } if($D2[$l2][1] < $D1[$l1][1]) { $ret[]=$D2[$l2]; $l2++; continue; } } while($l1<sizeof($D1)) $ret[]=$D1[$l1++]; while($l2<sizeof($D2)) $ret[]=$D2[$l2++]; return $ret; } function solution($X, $Y)
{ //echo implode(",",$X)."\n"; //echo implode(",",$Y)."\n"; $D=array(); foreach($X as $i => $x) { $y=$Y[$i]; $D[]=array($i,sqrt($x*$x+$y*$y)); } $D=quick_sort($D,0, sizeof($D)-1); $i0=$D[0][0]; $i1=$D[1][0]; //echo $X[$i0].",".$Y[$i0]." ".$X[$i1].",".$Y[$i1]."\n"; $m=(int)max(abs(($X[$i0]-$X[$i1])/2) , abs(($Y[$i0]-$Y[$i1])/2) ); for($i=2;$i<sizeof($D);$i++) { $ii=$D[$i][0]; $ii2=$D[$i-1][0]; $m2=(int)max(abs(($X[$ii]-$X[$ii2])/2) , abs(($Y[$ii2]-$Y[$ii])/2) ); if($m2<$m) $m=$m2; } return $m; }