# Javascript-given a value, for example, 100. given an array, pick out N elements from the array. the addition of these N elements is also 100. you can get a result.

Source: Internet
Author: User
For example, in the array: {code...}, find the array element: {code...} for the sum of 100, such as array:

``var arr = [99.1, 92.2, 60, 50,           49.5, 45.7, 25.1, 20,            17.4, 13, 10, 7, 2.1, 2, 1];``

Find the array element with the sum of 100:

``[60,20,10,7,2,1]``

For example, array:

``var arr = [99.1, 92.2, 60, 50,           49.5, 45.7, 25.1, 20,            17.4, 13, 10, 7, 2.1, 2, 1];``

Find the array element with the sum of 100:

``[60,20,10,7,2,1]``

``Function f (\$ n, \$ arr) {// \$ n is the target number, and \$ arr is an array composed of numbers if (empty (\$ arr) return false; // if the array has no elements, return if (in_array (\$ n, \$ arr) return [\$ n]; // if the expected value already exists, directly return the value foreach (\$ arr as \$ k => \$ v) {// traverse the array if (\$ v> \$ n) continue; // It is larger than the specified number, through \$ copy = \$ arr; // copy the array unset (\$ copy [\$ k]); // remove the selected number from the copy array \$ next = f (\$ n-\$ v, \$ copy); // recursively calculate if (! Empty (\$ next) return array_merge ([\$ v], \$ next); // merge result set} return false; // not found} \$ arr = [99.1, 92.2, 60, 50, 49.5, 45.7, 25.1, 20, 7.4, 13, 10, 7, 2.1, 2, 1]; \$ data = f (100, \$ arr ); print_r (\$ data); // Array ([0] => 60 [1] => 20 [2] => 13 [3] => 7) \$ data = f (105, \$ arr); print_r (\$ data ); // Array ([0] => 60 [1] => 20 [2] => 13 [3] => 10 [4] => 2)``

Coming Python version:

``def subsetsum(elements, target):    if target==0:        return True, []    elif not elements or target < 0:        return False, None    result, subset = subsetsum(elements[:-1], target-elements[-1])    return (True, subset + [elements[-1]]) if result else subsetsum(elements[:-1], target)``

The idea is very simple. when I want to ask`elements`Can I add?`target`There are only two possibilities:

1. I want to use`element[-1]`Can be added out`target`-> I want to be able to renew`elements[:-1]`Add out`target-elements[-1]`Yes

2. I do not need to use`element[-1]`You can add`target`-> I want to be able to renew`elements[:-1]`Add out`target`Yes

Boundary condition is:

1. When`target`For`0`It means that I can add anything without any need, so`return True, []`

2. When`elements`Null or`target`When the parameter value is set, it indicates that the value is never added.`return False, None`

Zookeeper:

``elements = [99.1, 92.2, 60, 50, 49.5, 45.7, 25.1, 20, 17.4, 13, 10, 7, 2.1, 2, 1]target = 100result, subset = subsetsum(elements, target)print(result, subset)``

Result:

``True [60, 20, 10, 7, 2, 1]``

In other words, I think this topic is very familiar, and it will be more interesting if I want to test the speed to solve it.

I have done some research in this area, and I have raised a problem of transformation. you can think about it as follows:

Today, we have set a set of multiple duplicates (representing an integer that is OK), which is called` elements`In order to assign another integer multiple duplicates`targets`, Explain whether there are several sub-Multi-replica sets. the elements of each sub-Multi-replica set and exactly one element in`targets`Target.

There is no difference in definition. I have provided an example:

``elements = (1,4,6,4,1)targets = (5,10,1)``

This example has a solution:

``(1,4) -> 5(4,6) -> 10(1) -> 1``

Note that`elements`The element in can only be used once!

Problem combination. Let's take a look at the original question of leetcode:

``````Function t100 (array) {var tempArray = array. concat ([]). sort (function (a, B) {return a-B;}); var temp = 0; var result = []; for (var I = 0; I

100) {tempArray. length = I ;}for (var I = 0; I

``````
` Recursive nested loop`
Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.