# Find how many pair elements sums up to N

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 , 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]

• Why is the result of the third test case 2? Commented May 11, 2016 at 22:52
• You don't mention that in the rules. We shouldn't have to infer the rules from the test cases. Commented May 11, 2016 at 22:56
• Why does the last test case return 2? Commented May 11, 2016 at 23:00
• Oh, it looks like you are looking for the number of disjoint pairs of numbers that sum to 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.
– xnor
Commented May 11, 2016 at 23:01
• This needs a much clearer spec. All current answers are invalid. Commented May 11, 2016 at 23:14

# Pyth, 9 bytes

/sM{.cSE2


Test suite

Takes the number and array newline separated.

Explanation:

/sM{.cSE2
/sM{.cSE2Q    Implicit variable introduction.
Implicit: Q = eval(input())
E      Take the array as input.
S       Sort the input.
.c  2     Form all ordered pairs.
{          Deduplicate.
sM           Map each to its sum.
/        Q    Count the number of occurrences of Q in the resulting list.

• This seems for fail for 2, [1, 1, 1, 1, 1] (the challenge was very unclear...) Commented May 11, 2016 at 23:19

# R, 48 bytes

function(x,y)sum(colSums(combn(unique(x),2))==y)

• Welcome to Programming Puzzles & Code Golf! This is a nice first answer. Functions don't need to be named--we allow unnamed function definitions as submissions--so you can save 2 bytes by removing f=. :) Commented May 11, 2016 at 22:55

# Pyth - 13 bytes

l{SMfqQsT.PE2


# Julia, 48 bytes

x->n->count(p->sum(p)==n,combinations(∪(x),2))


This is an anonymous function that accepts an array and an integer and returns an integer. To call it, assign it to a variable and call like f(x)(n).

We get all combinations of size 2 from the unique values in the input array and for each pair test whether the sum is equal to the input integer. We count all such cases and return the result.

Try it online! (includes all test cases)

## Matlab(38)

 @(a,b)nnz(sum(nchoosek([b,.5],2)')==a)

• the 0.5 part is a dodge for second test case

cropped by 2 bytes following Alex' comment.

• Functions don't have to be named; you can just use the @(a,b) handle and omit f=, saving 2 bytes. Commented May 11, 2016 at 22:38

# JavaScript (ES6), 1126759 52 bytes

(a,b)=>{c=0;for(i of a)a.map(j=>c+=i+j==b);return c}


Because of amazing for-of loops of ES6, I was able to shave off 42 bytes from my previous solution!

Update : Thanks to @user6188402 for helping me shave off 3 bytes!

Update : Thanks to @Bálint for helping me shave off 15 bytes!

Try it Online!

Note : This fails for repeating numbers.