Created
June 25, 2021 14:36
-
-
Save fajarwz/e947bf341bd7efad249135722e682d13 to your computer and use it in GitHub Desktop.
Search for 3 numbers that summable to zero using PHP
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 | |
function threeSummableToZero($arr) { | |
for($i = 0; $i < count($arr); $i++) { | |
for($j = 0; $j < count($arr); $j++) { | |
for($k = 0; $k < count($arr); $k++) { | |
if($i == $j || $j == $k || $i == $k) continue; | |
if($arr[$i] + $arr[$j] + $arr[$k] == 0) return "[".$arr[$i].", ".$arr[$j].", ".$arr[$k]."]"; | |
} | |
} | |
} | |
return "Not Found"; | |
} | |
echo threeSummableToZero([2, 6, 8, -5, 2, 9, 3]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment