How to calculate the sum of two numbers in an array equal to a fixed value in PHP

PHPz
Release: 2023-04-20 14:33:12
Original
665 people have browsed it

PHP is a very commonly used server-side scripting language. We often use arrays during the development process. In arrays, we will encounter some problems, such as how to calculate the combination of two numbers in the array that the sum of is equal to a fixed value. This is a very common problem, and this article will introduce how to solve it.

1. Problem description

Given an integer array nums and an integer target, find two numbers in the array whose sum is equal to target.

For example, given nums = [2, 7, 11, 15], target = 9, because nums [0] nums [1] = 2 7 = 9, so [0, 1] is returned.

2. Solution

We can use the double loop method to solve this problem, but the time complexity is too high and it is not efficient enough. Here is a more efficient method: using a hash table.

We can store the traversed numbers into the hash table and determine whether there is another number in the hash table that is equal to the target minus the current number. If it exists, you can get the sum of the two numbers as target.

The specific implementation is as follows:

function twoSum($nums, $target) {
    $map = [];
    $len = count($nums);

    for ($i = 0; $i < $len; $i++) {
        $diff = $target - $nums[$i];
        if (isset($map[$diff])) {
            return [$map[$diff], $i];
        }
        $map[$nums[$i]] = $i;
    }
    return [-1, -1];
}

$nums = [2, 7, 11, 15];
$target = 9;
$res = twoSum($nums, $target);
print_r($res);
Copy after login

3. Code description

1. First, we define a hash table $map and initialize it as an empty array.

2. Then, we define the array $nums and the target value $target, and $len is the array length.

3. Next, we use a for loop to traverse the array $nums. Each time a number is traversed, the result of subtracting the number from the target value $target is $diff.

4. If there is another number in $map that is equal to $diff, it means that the sum of two numbers is $target, and we can return the index of these two numbers.

5. If it does not exist, store the traversed number in $map and proceed to the next cycle.

6. Finally, if no number combination that meets the requirements is found during the entire loop, [-1, -1] will be returned.

4. Summary

This article introduces the use of hash tables to solve the problem that the sum of two numbers in a PHP array is equal to a fixed value. By using a hash table, we can find the number combination that meets the requirements in one loop, with a time complexity of O(n).

In addition, we also need to pay attention to some edge cases, such as the case where the array is empty and the length is 1.

I hope this article will be helpful to readers who are new to php.

The above is the detailed content of How to calculate the sum of two numbers in an array equal to a fixed value in PHP. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!