More formally, we consider n molecules with weights w0, ..., wn − 1. A detection is considered successful if there are many different indices I = {i1, ..., im} such that l ≤ wi1 + ... + wim ≤ u.
Due to the nature of the device, the difference between l and u is guaranteed to be greater than or equal to the difference in weights between the heaviest and lightest molecules. More formally, u - l ≥ wmax - wmin, where wmax = max (w0, ..., wn − 1) and wmin = min (w0, ..., wn − 1).
It is required to write a program that either finds any subset of molecules with a total weight belonging to an instrument detection interval, or determines that such a subset does not exist.
Implementation details
You should implement one function (method):
int [] solve (int l, int u, int [] w)- l and u: detection interval boundaries,
- w: molecular weights.
If the required subset exists, then the function must return an array of molecular indices that form any such subset. If there are several correct answers, return any of them.
If the required subset does not exist, then the function should return an empty array.
For the C programming language, the function signature is slightly different:
int solve (int l, int u, int [] w, int n, int [] result)
n: number of elements in w (i.e. number of molecules)
the remaining parameters are the same as described above.
Instead of returning an array consisting of m indices (as indicated above), the function should write the indices into the first m cells of the result array and then return m.
If the required subset does not exist, then the function should return 0, without writing anything to the result array.
Your program can write indices to the returned array (or to the result array for C) in any order.
Please use the provided file templates to clarify the implementation in your chosen programming language.
Examples
Example 1solve (15, 17, [6, 8, 8, 7])
In this example, there are four molecules with weights of 6, 8, 8, and 7. The device can detect subsets of molecules with a total weight of 15 to 17 inclusive. Please note that 17 - 15 ≥ 8 - 6. The total weight of molecules 1 and 3 is equal to w1 + w3 = 8 + 7 = 15, thus the function can return [1, 3]. Other possible correct answers are [1, 2] (w1 + w3 = 8 + 8 = 16) and [2, 3] (w1 + w3 = 8 + 7 = 15).
Example 2solve (14, 15, [5, 5, 6, 6])In this example, there are four molecules with weights of 5, 5, 6, and 6. It is required to find a subset with a total weight of 14 to 15 inclusive. Again, note that 15 - 14 ≥ 6 - 5. For this example, there is no subset of molecules with a total weight from 14 to 15, respectively, the function should return an empty array.
Example 3solve (10, 20, [15, 17, 16, 18])In this example, there are four molecules with weights of 15, 17, 16, and 18. It is required to find a subset with a total weight from 10 to 20 inclusive. Again, note that 20 - 10 ≥ 18 - 15. Any subset consisting of one element has a weight from 10 to 20, respectively, the possible correct answers are [0], [1], [2] and [3].
Grading system(9 points): 1 ≤ n ≤ 100, 1 ≤ wi ≤ 100, 1 ≤ u, l ≤ 1000, all wi are equal.
(10 points): 1 ≤ n ≤ 100, 1 ≤ wi, u, l ≤ 1000, and max (w0, ..., wn − 1) - min (w0, ..., wn − 1) ≤ 1.
(12 points): 1 ≤ n ≤ 100 and 1 ≤ wi, u, l ≤ 1000.
(15 points): 1 ≤ n ≤ 10 000 and 1 ≤ wi, u, l ≤ 10 000.
(23 points): 1 ≤ n ≤ 10 000 and 1 ≤ wi, u, l ≤ 500 000.
(31 points): 1 ≤ n ≤ 200000 and 1 ≤ wi, u, l <231.
Sample Tester ModuleThe validator receives data in the following format:
- Line 1: integers n, l, u.
- Line 2: n integers: w0, ..., wn − 1.