Skip to content

Instantly share code, notes, and snippets.

@fajarwz
Created June 25, 2021 14:36
Show Gist options
  • Save fajarwz/e947bf341bd7efad249135722e682d13 to your computer and use it in GitHub Desktop.
Save fajarwz/e947bf341bd7efad249135722e682d13 to your computer and use it in GitHub Desktop.
Search for 3 numbers that summable to zero using PHP
<?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