Ad
Code
Diff
  • function closestToZero(array $ints) {
      
      if(empty($ints))
        return 0;
      
      $closest = $ints[0];
      $init = abs($ints[0]) - 1;
      
      foreach($ints as $n){
        if( abs($n) - 1 <= $init )
        {
            $closest = $n;
            $init = abs($n) - 1;
        }
      }
    
      return $closest;
    }
    
    • function closestToZero(array $ints) {
    • if(empty($ints))
    • return 0;
    • $closest = $ints[0];
    • $init = abs($ints[0]) - 1;
    • foreach($ints as $n){
    • if( abs($n) - 1 <= $init )
    • {
    • $closest = $n;
    • $init = abs($n) - 1;
    • }
    • }
    • return $closest;
    • }
    • ?>