đź§© How to optimally find a twosum solution within a range plus/minus the given capacity?
TwoSum: Determine whether there are two items whose length will equal the total length while ensuring the same item cannot be used twice. This optimizes for runtime over memory.
 Time complexity: $O(n)$
 Space complexity: $O(n)$
Samples
Input: (4, 5, 2, 6)
 Total length:
10
 Expect:
true
Input: (4, 5, 2, 5)
 Total length:
10
 Expect:
true
Input: (4, 5, 2, 7)
 Total length:
10
 Expect:
false
Code
fun isLengthMatch(totalLength: Int, lengths: IntArray): Boolean {
handleErrors(totalLength, lengths)
val searchSet = hashSetOf<Int>()
for (length in lengths) {
val targetLength = totalLength  length
if (searchSet.contains(targetLength)) return true
else searchSet.add(length)
}
return false
}
fun handleErrors(totalLength: Int, lengths: IntArray) {
if (totalLength <= 0) throw IllegalArgumentException(""totalLength" must be greater than 0.")
if (lengths.size < 2) throw IllegalArgumentException(""lengths" cannot be less than two items.")
}

Generate a
targetLengthsArray
+/ thetotalLength
and the given range allowance. 
Check if the
searchSet
contains any of the values in thetargetLengthsArray
.