0
How to do this without the need to make two bonds.
Sample method:
function getPairNumbers($array, $valor) {
//aqui viria o método
}
0
How to do this without the need to make two bonds.
Sample method:
function getPairNumbers($array, $valor) {
//aqui viria o método
}
0
You can test each position, start and end of the array, and test each position, and increase as you find the desired value:
<?php
function getPairNumbers($numbers, $target)
{
$result = array();
sort($numbers);
$high = count($numbers) - 1;
for ($i = 0; $i < count($numbers);) {
if ($numbers[$i] + $numbers[$high] == $target) {
$result[0] = $numbers[$i];
$result[1] = $numbers[$high];
break;
}
if ($numbers[$i] + $numbers[$high] < $target) {
$i++;
}
if ($numbers[$i] + $numbers[$high] > $target) {
$high--;
}
if ($high <= $i) {
return null;
}
}
return $result;
}
$arr = getPairNumbers(array(1,4,5,15,30,25,20,0,13), 18);
print_r($arr);
Browser other questions tagged php loop mathematics
You are not signed in. Login or sign up in order to post.