3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php final class ArrayUtils { private static $visited = []; private static $sum = 0; private static $data = []; public static function containsSum($data, $sum) { self::$data = $data; self::$sum = $sum; self::$visited = []; return self::found(); } private static function found() { if (! self::$data) { return false; } $value = array_pop(self::$data); if (isset(self::$visited[self::$sum - $value])) { return true; } self::$visited[$value] = true; return self::found(); } } $data = range(0, 49999); shuffle($data); var_dump(ArrayUtils::containsSum($data, 1220)); var_dump(ArrayUtils::containsSum($data, -7));
based on VJf7L
Output for 5.6.38 - 7.3.0rc3
bool(true) bool(false)