Two-Sum - Range Allowance Algorithm Design 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 because 4 + 6 equal 10.

2.

Input: [4, 5, 2, 5]
Total capacity: 12
Range Allowance: 2
Expect: true even though there are no two numbers that equal 12 directly, there are two numbers that sum to 10 which is within the range allowance provided.

3.

Input: [4, 5, 2, 7]
Total capacity: 14
Range Allowance: 1
Expect: false
Create a Set searchSet to store the item's that have already been examined.

Iterate through the input Array of item capacities.
2a. Find the targetCapacity for the current item: totalCapacity - itemCapacity.
2b. Generate a targetCapacitiesArray plus and minus the totalCapacity and the range allowance provided.
2c. For each capacity in targetCapacitiesArray, see if each number is contained in the searchSet, and return true if found.
2d. Else, add the itemCapacity to the searchSet.

Return false if the entire input is iterated through without finding a match.