Skip to content

Instantly share code, notes, and snippets.

@tieppt
Created June 3, 2016 09:03
Show Gist options
  • Save tieppt/af769b351ee61c91722f91c2a62095e9 to your computer and use it in GitHub Desktop.
Save tieppt/af769b351ee61c91722f91c2a62095e9 to your computer and use it in GitHub Desktop.
<?php
//$arr = array(10, -1, 10, -1, 4, 5, 0);
$arr = array(-3, 0, -4);
$brr = array();
for($i = 0, $n = count($arr); $i < $n; ++$i) {
$a = abs($arr[$i]);
if ($a) {
$brr[] = $a;
}
}
sort($brr);
$rs = count($brr) > 0 ? $brr[0] : 0;
$as = $rs;
if ($rs) {
$neg = true;
for($i = 0, $n = count($arr); $i < $n; ++$i) {
$j = $arr[$i];
$a = abs($j);
if ($a == $rs) {
$as = $j;
if ($j > 0 && $neg) {
$neg = false;
break;
}
}
}
}
echo $as;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment