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(array $data, int $sum): bool { self::$visited = []; self::$sum = $sum; return array_reduce($data, 'ArrayUtils::reduce', false); } private static function reduce(bool $carry, int $item): bool { $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, 1220)); var_dump(ArrayUtils::containsSum($data, -7));
based on OQOip