6
\$\begingroup\$

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]
\$\endgroup\$
  • \$\begingroup\$ Why is the result of the third test case 2? \$\endgroup\$ – Dennis May 11 '16 at 22:52
  • 3
    \$\begingroup\$ You don't mention that in the rules. We shouldn't have to infer the rules from the test cases. \$\endgroup\$ – Dennis May 11 '16 at 22:56
  • 1
    \$\begingroup\$ Why does the last test case return 2? \$\endgroup\$ – Dennis May 11 '16 at 23:00
  • 3
    \$\begingroup\$ 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. \$\endgroup\$ – xnor May 11 '16 at 23:01
  • 6
    \$\begingroup\$ This needs a much clearer spec. All current answers are invalid. \$\endgroup\$ – Dennis May 11 '16 at 23:14
4
\$\begingroup\$

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.
\$\endgroup\$
  • \$\begingroup\$ This seems for fail for 2, [1, 1, 1, 1, 1] (the challenge was very unclear...) \$\endgroup\$ – Luis Mendo May 11 '16 at 23:19
4
\$\begingroup\$

R, 48 bytes

function(x,y)sum(colSums(combn(unique(x),2))==y)
\$\endgroup\$
  • 4
    \$\begingroup\$ 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=. :) \$\endgroup\$ – Alex A. May 11 '16 at 22:55
1
\$\begingroup\$

Pyth - 13 bytes

l{SMfqQsT.PE2

Test Suite.

\$\endgroup\$
1
\$\begingroup\$

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)

\$\endgroup\$
1
\$\begingroup\$

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.

\$\endgroup\$
  • 3
    \$\begingroup\$ Functions don't have to be named; you can just use the @(a,b) handle and omit f=, saving 2 bytes. \$\endgroup\$ – Alex A. May 11 '16 at 22:38
1
\$\begingroup\$

JavaScript (ES6), 112 67 59 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.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.