Created
June 3, 2016 09:03
-
-
Save tieppt/af769b351ee61c91722f91c2a62095e9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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