This is my naive approach:
$sample1 = [3, -20, 1, -19, -4, 6, 77, -1, 0, 6];
$sample3 = [4, 5, -7, 0, 2, 44, -3, -5, 66, -7];
$sample4 = [4, 25, 1, 6, 9, 5, 999, 4, 43, 2];
$sample2 = [];
for ($i = 0; $i < mt_rand(10, 30); $i++) {
$sample2[] = mt_rand(-1000, 1000);
}
extractNumbersThatSquaresAreGreaterThan($sample1);
extractNumbersThatSquaresAreGreaterThan($sample2);
extractNumbersThatSquaresAreGreaterThan($sample3);
extractNumbersThatSquaresAreGreaterThan($sample4);
var_dump($sample1);
//var_dump($sample2);
var_dump($sample3);
var_dump($sample4);
function extractNumbersThatSquaresAreGreaterThan(&$list, $boundary = 26) {
foreach ($list as $key => $value) {
if (pow($value, 2) <= $boundary) {
unset($list[$key]);
}
}
}
Any hints for optimization? Is it possible, I've tried few, but in the end it was more complicated (and slower) solution