3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php final class ArrayUtils { private static $visited = []; private static $sum = 0; public static function containsSum($data, $sum) { self::$visited = []; self::$sum = $sum; return array_reduce($data, 'ArrayUtils::reduce', false); } private static function reduce($carry, $item) { $carry |= isset(self::$visited[self::$sum - $item]); self::$visited[$item] = true; return (bool) $carry; } } $data = range(0, 49999); shuffle($data); var_dump(ArrayUtils::containsSum($data, 1234)); var_dump(ArrayUtils::containsSum($data, -15));
based on d8MWA
Output for 5.6.38 - 7.3.0rc3
bool(true) bool(false)