🧩 What is the best way to find if there are two individual capacities that sum to a total capacity within a range of plus and minus the total capacity?
Optimize for runtime over memory complexity.
The same item cannot be selected twice.
Inputs
- An Int representing the total capacity
- An Int representing the range above and below the total capacity sums are allowed to be within.
- An Array of Int's representing items' individual capacities.
Output: A Boolean representing whether it is possible for two of the items to equal the total capacity plus and minus the given range.
Samples
1.
- Input:
[4, 5, 2, 6]
- Total capacity:
10
- Range Allowance:
0
- Expect:
true
because4
+6
equal10
.
2.
- Input:
[4, 5, 2, 5]
- Total capacity:
12
- Range Allowance:
2
- Expect:
true
even though there are no two numbers that equal12
directly, there are two numbers that sum to10
which is within the range allowance provided.
3.
- Input:
[4, 5, 2, 7]
- Total capacity:
14
- Range Allowance:
1
- Expect:
false