[Question]
  [
**This question already has answers here**:
 
 
[Find two integers from an unordered list to sum to the input](/questions/113365/find-two-integers-from-an-unordered-list-to-sum-to-the-input)
 (26 answers)
 
Closed 6 years ago.
# The Task
This is my first challenge so apologies if it is very simple! In this challenge, your task is to write a program or function which takes in a list of integers and a desired sum and outputs a truthy or falsey value based on whether the list contains two numbers that can be summed together to achieve the desired sum.
# Input
The input must be acquired in a standard way for your language. The list of integers can be any standard collection type for your language but cannot be a hashed set, sorted set, or any highly functional collection type. Don't assume the input is sorted, any valid integer type is acceptable.
The desired sum is also an integer and must be provided to your program as input.
# Output
The output may be a function return, console log, writing to a file, etc... Any valid Truthy or Falsey value is acceptable.
# Additional Rules
* The input list may or may not be sorted.
* Duplicate values may occur in the list
* The same item in the list may not be summed to itself to achieve the desired sum (Eg. [3,4,5] with desired sum 6 will expect False because 3 cannot be used twice)
* Standard loopholes are not allowed.
Test Cases
```
INPUT                 OUTPUT
[3,4,5], 6            Falsey
[1,2,3,4,5,6],11      Truthy
[4,23,8174,42,-1],41  Truthy
[1,1,1,1,1,3],3       Falsey
[2,1,1,1,53,2],4      Truthy
[2,2,2,2],4           Truthy
[3],3                 Falsey
[],-43                Falsey
```
This is [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'"), so the shortest code in bytes wins!
  
[Answer]
# [Brachylog](https://github.com/JCumin/Brachylog), 3 bytes
```
⊇Ċ+
```
[Try it online!](https://tio.run/nexus/brachylog2#@/@oq/1Il/b//9GGOkY6xjomOqY6ZrH/DQ0B "Brachylog – TIO Nexus")
```
⊇Ċ+   input as STDIN, output as ARG1
      (prove that:)
⊇     the input is a superset of
 Ċ       a list of two elements
  +         which sums up to the output
```
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), ~~7~~ ~~6~~ 5 bytes
```
ŒcS€ċ
```
[Try it online!](https://tio.run/nexus/jelly#@390UnLwo6Y1R7r///8fbaJjZKxjYWhuomNipKNrGPvfxBAA "Jelly – TIO Nexus")
-1 byte by using the Unordered Pairs atom
```
ŒcS€ċ  Main Link; left argument is the array, right argument is the required sum
  S€   Take the sum of each...
Œc     ...unordered pair...
    ċ  Check if the sum array contains the right argument
```
-1 byte thanks to Erik!
# 4 bytes
```
Œc§i
```
This does the same thing but with a newer language feature of vectorize-at-depth-1 sum. Thanks Dennis! :D Credit to [@Riolku](https://codegolf.stackexchange.com/users/90304/riolku) for noticing this.
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E), 6 bytes
```
æ2ùOIå
```
[Try it online!](https://tio.run/nexus/05ab1e#@394mdHhnf6eh5f@/x9tomNkrGNhaG6iY2Kko2sYy2ViCAA "05AB1E – TIO Nexus")
**Explanation**
```
æ        # get powerset of first input
 2ù      # keep only those of size 2
   O     # sum each
    Iå   # check if 2nd input is in the list of sums
```
[Answer]
# Mathematica, 29 bytes
```
!FreeQ[Tr/@#~Subsets~{2},#2]&
```
[Answer]
# [Python 2](https://docs.python.org/2/), ~~43~~ 42 bytes
-1 byte thanks to mbomb007
```
lambda a,b:any(b-a.pop()in a for x in a*1)
```
[Try it online!](https://tio.run/nexus/python2#NYpBDoIwFAX3nOIvW/Iw@W1BQ@JJahcFITTR0hCMenDXKKiZzbyX6Y@n5eKvzdmTR1P7@BRN4XdpTEKGSJ76caIHrZqzXO5DuHTEdZpCnKkXeYjpNgspF6thUDpQlVmGwjZROTBn1kBpHHhvYBQKdjC8Vn@0g86s@q1SQ32K9djY/Js4FEa/4li0vh26Nw "Python 2 – TIO Nexus")
[Answer]
# [Haskell](https://www.haskell.org/), ~~39 37~~ 36 bytes
```
(a:t)#n=n`elem`map(a+)t||t#n
_#n=1<0
```
[Try it online!](https://tio.run/nexus/haskell#NYpBCsIwEEX3niIwmxa/4CRpFbFH8AQh2Cy6KJihlCx79zgE5a/ee7926VF6kknm5bPkOaetS@e@HEchOb018PNac1rFTEbjy2z7KsWE4OAxRBphAsOiIcZIzGo8rMOdbx7e4sKRPLfffy6SU2F/ODhY/TTTphDrFw "Haskell – TIO Nexus") Example usage: `[1,3,2,5] # 3`. Returns `True` or `False`.
---
**Alternative** (also 36 bytes)
```
f(a:t)=map(a+)t++f t
f e=e
(.f).elem
```
[Answer]
# [MATL](https://github.com/lmendo/MATL), 6 bytes
```
2XN!sm
```
[Try it online!](https://tio.run/nexus/matl#NYq7DYAwFAN7b0HvFO@TwADUVBRI0ZNYgIr9FSCArvFZtzfdluE82ry2anTmQEEVKruxBERQnWqcZHS6MknA5al@LGCo@lk26l08R6fvNwkktws "MATL – TIO Nexus")
### Explanation
```
2XN   % Implicitly input an array. Combinations of elements taken 2 at a time
      % The result is a matrix where each row is a combination
!s    % Sum of each row
m     % Implicitly input a number. Ismember function. Implicitly display
```
[Answer]
# C, ~~114~~ 113 bytes
```
i,j,k="F";main(c,v)char**v;{for(--c;++i<c;)for(j=0;++j<c;)k=j^i&atoi(v[i])+atoi(v[j])==atoi(v[c])?"T":k;puts(k);}
```
Here's the output
```
C:\eng\golf>a.exe 3 4 5 6
F
C:\eng\golf>a.exe 1 2 3 4 5 6 11
T
C:\eng\golf>a.exe 4 23 8174 42 -1 41
T
C:\eng\golf>a.exe 1 1 1 1 1 3
F
C:\eng\golf>a.exe 2 1 1 1 53 2 4
T
C:\eng\golf>a.exe 2 2 2 2 4
T
```
[Answer]
# JavaScript (ES6), ~~38~~ 43 bytes
```
a=>n=>[...a].some(_=>a.includes(n-a.pop()))
```
---
## Try It
```
f=
a=>n=>[...a].some(_=>a.includes(n-a.pop()))
o.innerText=`           f([3,4,5])(6) = ${f([3,4,5])(6)}
    f([1,2,3,4,5,6])(11) = ${f([1,2,3,4,5,6])(11)}
f([4,23,8174,42,-1])(41) = ${f([4,23,8174,42,-1])(41)}
     f([1,1,1,1,1,3])(3) = ${f([1,1,1,1,1,3])(3)}
    f([2,1,1,1,53,2])(4) = ${f([2,1,1,1,53,2])(4)}
         f([2,2,2,2])(4) = ${f([2,2,2,2])(4)}
               f([3])(3) = ${f([3])(3)}
              f([])(-43) = ${f([])(-43)}
     f([1,2,3,4,5,6])(3) = ${f([1,2,3,4,5,6])(3)}`
```
```
pre{font-size:14px;line-height:1.75}
```
```
<pre id=o>
```
[Answer]
# Ruby, ~~59~~ ~~53~~ 37 bytes
[inspiration comes from this answer](https://codegolf.stackexchange.com/a/120806/46172)
```
->a,b{a.map{|e|a.include?(b-e)}.any?}
```
will now look where can I chop a few bytes -))
**HISTORY**
```
#53 bytes
->a,b{a.combination(2).map{|e|e.reduce(&:+)==b}.any?}
#59 bytes 
->a,b{a.combination(2).map{|e|e.reduce(&:+)}.any?{|x|x==b}}
```
[Answer]
## Clojure, 53 bytes
```
#(loop[[v & r]%](if v(or((set r)(- %2 v))(recur r))))
```
Uses destructuring to take the first and rest of the input list (and subsequent `recur` of rest), and returns the number "target - value1" if found in the set of remaining numbers (a "truthy"), nil otherwise.
[Answer]
# Java 7, 138 bytes
```
boolean c(int[]a,int s){String r="";for(int l=a.length,i=0,j;i<l;i++)for(j=-1;++j<l;)if(i!=j)r+=(a[i]+a[j])+",";return r.contains(s+",");}
```
**Explanation:**
```
boolean c(int[]a,int s){             // Method with integer-array and integer parameters and boolean return-type
  String r="";                       // Temp String
  for(int l=a.length,i=0,j;i<l;i++)  //  Loop (1) over the input array
    for(j=-1;++j<l;)                 //   Inner loop (2) over the input array again
      if(i!=j)                       //    If the index of both loops aren't equal (so not the same item in the list)
        r+=(a[i]+a[j])+",";          //     Append the sum with a comma to the temp-String
                                     //   End of loop (2) (implicit / single-line body)
                                     //  End of loop (1) (implicit / single-line body)
  return r.contains(s+",");          //  Return whether the temp-String contains the given sum (+ comma)
}                                    // End of method
```
**Test code:**
[Try it here.](https://tio.run/nexus/java-openjdk#jZBBbsMgEEX3OcXUKxATq9hOWolyhK6ytLwgrpNgEYgAt6oszp7aTrutrL9AmvmP@TOtUSHA@7gBCFFF3cL96JzplIWWaBvrRuH0QKDjIXptz@BllomT83MXjFS56ew5XlDLZ@yFfjNCM0ZnQy@3XDDWTyWqT0Q/yZ56JomqdcNU3TeUZZgJ38XBW/B562xU2gYS5joV6Q5wG45mCvWb7dPpD7hOHvIIUzeg6Jwd4PAdYnfN3RDz29SKxpKW2O4LliVGjgWWWOEO9wk5p1SsoSosSnzlLxVWBW55wmotWSBftCuxmLDV1KL/gXVfcfxTmbBcOX@5UML9w5426f4D)
```
class M{
  static boolean c(int[]a,int s){String r="";for(int l=a.length,i=0,j;i<l;i++)for(j=-1;++j<l;)if(i!=j)r+=(a[i]+a[j])+",";return r.contains(s+",");}
  
  public static void main(String[] a){
    System.out.println(c(new int[]{1,2,3,4,5,6},11));
    System.out.println(c(new int[]{4,23,8174,42,-1},41));
    System.out.println(c(new int[]{2,1,1,1,53,2},4));
    System.out.println(c(new int[]{2,2,2,2},4));
    System.out.println();
    System.out.println(c(new int[]{1,1,1,1,1,3},3));
    System.out.println(c(new int[]{3,4,5},6));
  }
}
```
**Output:**
```
true
true
true
true
false
false
```
[Answer]
# [TAESGL](https://github.com/tomdevs/taesgl), 8 bytes
```
Ş⇒AĨB-AĖ
```
[**Interpreter**](https://tomdevs.github.io/TAESGL/v/1.2.0/index.html?code=XHUwMTVlXHUyMWQyQVx1MDEyOEItQVx1MDExNg==&input=WzQsMjMsODE3NCw0MiwtMV0sNDE=) 
**Explanation**
```
Ş⇒        Array.some with anonymous function on implicit input
  AĨ      first input includes
    B-    second input minus
      AĖ  first input popped
```
[Answer]
## PHP (>= 7.1), ~~53~~ ~~51~~ ~~50~~ 46 Bytes
```
<?foreach($_GET[0]as$c)+$$c||${$_GET[1]-$c}=a;
```
sadly it's quite big but at least better than 2 loops. `+a` fails with a warning if the sum can be made => non empty output => *truthy*. Outputs nothing if the sum can not be made => *falsey*.
Thanks @user63956 !
[Answer]
## Mathematica, 38 bytes
```
MemberQ[Total/@#~Permutations~{2},#2]&
```
input style [{1,2,3,4,5,6},11]
[Answer]
# [Python 3](https://docs.python.org/3/), 41 bytes
```
f=lambda a,b,*c:a-b in c or c and f(a,*c)
```
[Try it online!](https://tio.run/nexus/python3#NY5LCsMwDET3PYWWdpks/ElbAj2J8EJOMARat4Te35WbFAnBvHkLtXJ/yDMvQoKM8zzJkGmtNNNr0yN1oWJEC9uKEpV6y8wBEWMCXRKYHTx@ABqd6yjCB9zcNSJ6DIqi283/hNSX2R95DPBqpTSd6L2t9WOK0Y/E2vYF "Python 3 – TIO Nexus")  
Recursive aproach with *flexible* input and output. Returns an empty set as falsy.
[Answer]
# R, 32 bytes
```
function(x,l)x%in%combn(l,2,sum)
```
Returns an anonymous function. `combn` returns all combinations of `l` taken `m` at a time, in this case 2, and then optionally applies a function, in this case `sum`. Returns an empty logical vector `logical(0)` for an empty first input, which I define for this problem as falsey, and `TRUE` or `FALSE` corresponding to the other elements otherwise.
[Try it online!](https://tio.run/nexus/r#S7P9n1aal1ySmZ@nUaGTo1mhmpmnmpyfm5SnkaNjpFNcmqv5P03DSCdZwxDINdYx0THV1PwPAA)
[Answer]
# Pyth - 9 bytes
```
/msd.cE2E
```
[Try it](https://pyth.herokuapp.com/?code=%2Fmsd.cE2E&input=%5B3%2C4%2C5%5D6%20%20%20%20%20%20%20%20%20%20%20%20Falsey%0A%5B1%2C2%2C3%2C4%2C5%2C6%5D%2C11%20%20%20%20%20%20Truthy%0A%5B4%2C23%2C8174%2C42%2C-1%5D%2C41%20%20Truthy%0A%5B1%2C1%2C1%2C1%2C1%2C3%5D%2C3%20%20%20%20%20%20%20Falsey%0A%5B2%2C1%2C1%2C1%2C53%2C2%5D%2C4%20%20%20%20%20%20Truthy%0A%5B2%2C2%2C2%2C2%5D%2C4%20%20%20%20%20%20%20%20%20%20%20Truthy%0A%5B3%5D%2C3%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Falsey%0A%5B%5D%2C-43%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Falsey&test_suite=1&test_suite_input=%5B3%2C4%2C5%5D%0A6%0A%5B1%2C2%2C3%2C4%2C5%2C6%5D%0A11%0A%5B4%2C23%2C8174%2C42%2C-1%5D%0A41%0A%5B1%2C1%2C1%2C1%2C1%2C3%5D%0A3%0A%5B2%2C1%2C1%2C1%2C53%2C2%5D%0A4%0A%5B2%2C2%2C2%2C2%5D%0A4%0A%5B3%5D%0A3%0A%5B%5D%0A-43&debug=0&input_size=2)
```
/msd.cE2E
    .cE2     # All subsets of length 2
 msd         # The sum of each subset
/       E    # Number of occurrences of the input number
```
[Answer]
# MATLAB, 28 bytes
```
@(a,b)any(pdist(a,@plus)==b)
```
This defines an anonymous function with inputs `a`: column vector (possibly empty); `b`: number.
The code also works in Octave except if the first input is empty, which causes an error. **[Try it online!](https://tio.run/nexus/octave#RYrLCsIwEEX3fkWXCaSBebQKl0D/o3TRIgVBpFBd@PVxDLFyN3POnDXFGPPg5rD4@fF22/W2P42G7f7afUqLz6sbBYpuakLvT0YERjHop9AQFalgwYXOCmW0ZA@lWv8mJqU4rqYT8Lessuzgf25Hq@LzBw)**
### Explanation
`pdist(a,@fun)` applies function `fun` (in our case, addition) to each combination of two rows of the input matrix `a` (in our case, a column vector). The output is a column vector. `any(...==b)` gives true if any of the results equals the input number `b`. 
[Answer]
# Python 2, 71 bytes
```
from itertools import*
lambda L,n:n in map(sum,list(combinations(L,2)))
```
[**Try it online**](https://tio.run/nexus/python2#NYpNDoIwGAX3PUWXLXks@gMaEm/ADbCLqhC/hLYESjw@Cmpm8@Zlhst1G324PTxvEZvIKfLgJ7GsASMtWdxTuFH0mVJcRAstpWTDnAKn3M85pXHhFKY052J7PWnsuWqmmWLmgygoTmsWUm6dgUXlwGvWKWgcitpBKdZZaIOzOllYjVI5WLVXf4yDYZ3@WWWgP8V@HBz7mziU1rwB)
Yes, I know there is already a better solution posted.
[Answer]
## C#, ~~96~~ 90 bytes
```
using System.Linq;a=>n=>Enumerable.Range(0,a.Count).Any(i=>a.Skip(i+1).Any(j=>a[i]+j==n));
```
Compiles to a `Func<List<int>, Func<int, bool>>`. Formatted version:
```
Func<List<int>, Func<int, bool>> f = a => n =>
    Enumerable.Range(0, a.Count).Any(i => a.Skip(i + 1).Any(j => a[i] + j == n));
```
Old `for` loop version for 96 bytes:
```
a=>n=>{for(int i=0,j,l=a.Count;i<l;++i)for(j=i+1;j<l;)if(a[i]+a[j++]==n)return 1>0;return 1<0;};
```
[Answer]
# [AWK](https://www.gnu.org/software/gawk/manual/gawk.html), 77 bytes
```
{for(s=i=0;++i<NF;)for(j=0;++j<NF;){if(i==j)continue
if($i+$j==$NF)s=1}$0=s}1
```
[Try it online!](https://tio.run/nexus/awk#VY2xDoMwDER3vuKGDCCEhBPTVqJeGfmJqkjJAFIpE@Lb05DQitoefO/Osl@H6ZXPYqVuy9Le@64tduKidlGvdsitiCse0/i24/LMAlC2VE5E9V0xC22qlnkj7w0YDXDBqTIQNJITDKIEGdrgRlcGa1QBMu3JbxvsE5P6QI0Ja7g7YOqkE/vdnH5X/I@yDw "AWK – TIO Nexus")
I could save 2 bytes by removing the `i=` in the `for(s=i=0...)`, but then it would only work for one line of data. Since this isn't the shortest answer, I thought I'd make a general solution :)
Input is whitespace separated numbers with the last value being the desired sum.
[Answer]
# [Jellyfish](https://github.com/iatorm/jellyfish), 13 bytes
```
p
cd!i
i 2
 1
```
[Try it online!](https://tio.run/nexus/jellyfish#@1/AlZyimMmVqWDEpWD4/3@0iYKRsYKFobmJgomRgq5hLJeJIQA "Jellyfish – TIO Nexus")
## How it works
```
print(exists(i,from_base(1,permutations(i,2)))
```
where `i` are the inputs.
[Answer]
# Axiom, ~~90~~ 92 bytes
```
f(a,k)==(for i in 1..#a-1 repeat for j in i+1..#a repeat(q:=a.i+a.j;q=k=>return true);false)
```
test 
```
(42) -> f([3,4,5],6)
   (42)  false
                                                            Type: Boolean
(43) -> f([1,2,3,4,5,6],11)
   (43)  true
                                                            Type: Boolean
(44) -> f([4,23,8174,42,-1],11)
   (44)  false
                                                            Type: Boolean
(45) -> f([4,23,8174,42,-1],41)
   (45)  true
                                                            Type: Boolean
(46) -> f([2,23,8174,42,2],2)
   (46)  false
```
[Answer]
## [C#](http://csharppad.com/gist/e11a62dffa6ce06e38335c5da1f8ac52), ~~103~~ 102 bytes
---
### Data
* **Input** `Int32[]` `l` A list of `Int32` to sum
* **Input** `Int32` `r` The result to check against
* **Output** `Boolean` A result indicating if the sum can be performed
---
### Golfed
```
(int[] l,int r)=>{for(int x=l.Length,y;--x>=0;)for(y=0;y<x;)if(l[x]+l[y++]==r)return 1>0;return 0>1;};
```
---
### Ungolfed
```
( int[] l, int r ) => {
   for( int x = l.Length, y; --x >= 0; )
      for( y = 0; y < x; )
         if( l[ x ] + l[ y++ ] == r )
            return 1 > 0;
   
   return 0 > 1;
};
```
---
### Ungolfed readable
*`Working on it...`*
---
### Full code
```
using System;
using System.Collections.Generic;
namespace Namespace {
   class Program {
      static void Main( String[] args ) {
        Func<Int32[], Int32, Boolean> f = ( int[] l, int r ) => {
            for( int x = l.Length, y; --x >= 0; )
                for( y = 0; y < x; )
                    if( l[ x ] + l[ y++ ] == r )
                        return 1 > 0;
   
            return 0 > 1;
        };
        List<KeyValuePair<Int32[], Int32>>
            testCases = new List<KeyValuePair<Int32[], Int32>>() {
                new KeyValuePair<Int32[], Int32>( new Int32[] { 3, 4, 5 }, 6 ),
                new KeyValuePair<Int32[], Int32>( new Int32[] { 1, 2, 3, 4, 5, 6 }, 11 ),
                new KeyValuePair<Int32[], Int32>( new Int32[] { 4, 23, 8174, 42, -1 }, 41 ),
                new KeyValuePair<Int32[], Int32>( new Int32[] { 1, 1, 1, 1, 1, 3 }, 3 ),
                new KeyValuePair<Int32[], Int32>( new Int32[] { 2, 1, 1, 1, 53, 2 }, 4 ),
                new KeyValuePair<Int32[], Int32>( new Int32[] { 2, 2, 2, 2 }, 4 ),
                new KeyValuePair<Int32[], Int32>( new Int32[] { 3 }, 3 ),
                new KeyValuePair<Int32[], Int32>( new Int32[] {  }, -43 ),
            };
        foreach( var testCase in testCases ) {
            Console.WriteLine( $" Input: {testCase.Value}; {{ {String.Join(", ", testCase.Key)} }}\nOutput: {f( testCase.Key, testCase.Value )}\n" );
        }
         Console.ReadLine();
      }
   }
}
```
---
### Releases
* **v1.1** - `-1 byte` - Thanks to [TheLethalCoder](https://codegolf.stackexchange.com/posts/comments/296698?noredirect=1) suggestion.
* **v1.0** - `103 bytes` - Initial solution.
---
### Notes
* None
]