Based on a recent question in StackOverflow
Write a program/function that takes in one ARRAY/list and returns the number of pair-values which sums up to a given TOTAL.
Explaining
TOTAL=4, ARRAY=[1,3,7,1,-3] => RESULT=2
The result was 2 because 1 + 3
and 7 + -3
are valid pairs which sums up to TOTAL=4
.
- When one pair is formed, you should not count any of its values again
- Also, you are not allowed to add any value that is not present in the ARRAY.
This is code-golf, shortest in bytes wins
All values will be between your language's int min-max.
Another cases
9, [1,2,3,5,4,6,7,-10] => 3 // [2,7] [3,6] [5,4]
0, [] => 0 //
0, [1,-2,0,-1,2] => 2 // [1,-1] [-2,2]
5, [5,5,5,0,-1,1,-5] => 1 // [5,0]
3, [7,-7,8,3,9,1,-1,5] => 0 //
2, [1,1] => 1 // [1,1]
2, [1,1,1,1,1] => 2 // [1,1] [1,1]
4, [1,3,3,3] => 1 // [1,3]
n
. When you say "number of pair-values", people usually think of that as the number of choices that can be made, even if they overlap. \$\endgroup\$