Home > database >  How do I write a function in PHP that takes an array and returns an array if any two values in the i
How do I write a function in PHP that takes an array and returns an array if any two values in the i

Time:08-02

function twoNumberSum($array,$targetSum) {
   sort($array);
   $left = 0;
   $right = count($array) - 1;
   while ($left < $right) {
       $currentSum = $array[$left]   $array[$right];
       if($currentSum == $targetSum) {
            print_r([$array[$left], $array[$right]]);
       } else if ($currentSum < $targetSum) {
           $left  = 1;
       } else if ($currentSum > $targetSum) {
           $right -= 1;
       }
   }
   return [];
}

    $numArray = [3,5,-4,8,11,1,-1,6];
    $targetSum = 10;
    twoNumberSum($numArray,$targetSum);

The Goal

My goal here is to use pointers to slowly work through the array on both ends, all the while adding the values at array[left] and array[right]. If the currentSum is less than the targetSum, the left pointer moves up by one. If the currentSum is greater than the targetSum, the right pointer goes down by one. If currentSum and targetSum are equal, the function returns an array of array[left] and array[right].

The Problem

For some reason, when I run the code with the above array and target value, with the expected output being (11, -1), I get the expected output, but it's repeated several times over. I actually wouldn't encourage you all to run this code on your machines. I'll include a screenshot of what I'm seeing. output from above code

What I've tried

I've tried exiting the function using the return keyword beneath where I'm calling the print_r function. And this works, but only to a certain degree. In the event that targetValue is equal to 4, in which case the expected output is (3,1) (5,-1), and (-4,8), the output only returns the last valid pair of integers rather than all of them.

Any help in figuring this out would be greatly appreciated.

CodePudding user response:

cycle through the array, and for each element, cycle through the rest of the array to test sums.

function twoNumberSum ( $inputArray, $targetSum ) {
    for ( $i = 0; $i < count( $inputArray ) - 1; $i   ) {
        for ( $e = $i   1; $e < count( $inputArray ); $e   ) {
            if ( $inputArray[$i]   $inputArray[$e] == $targetSum ) return [];
        }
    }
    return null;
}

$numArray = [3,5,-4,8,11,1,-1,6];
$targetSum = 10;
var_dump( twoNumberSum( $numArray, $targetSum ) );

CodePudding user response:

To compare all elements of the array with each other, 2 loops are necessary. This solution uses 2 forech loops and returns all possible pairs that satisfy the condition. There is no need to manually create a copy of $inputArray as PHP forech works internally with copies.

function twoNumberSum ( $inputArray, $targetSum ) {
    $res = [];
    foreach($inputArray as $i => $firstVal){
        array_shift($inputArray);
        foreach($inputArray as $secVal){
            if ( $firstVal   $secVal == $targetSum ) {
              $res[] = [$firstVal,$secVal] ;
            }
        }
    }
    return $res;
}

$numArray = [3,5,-4,8,11,1,-1,6];
$targetSum = 4;

var_export( twoNumberSum( $numArray, $targetSum ) );

Output:

array (
  0 => 
  array (
    0 => 3,
    1 => 1,
  ),
  1 => 
  array (
    0 => 5,
    1 => -1,
  ),
  2 => 
  array (
    0 => -4,
    1 => 8,
  ),
)

Try self.

  • Related