Given a list and a number k return whether any two numbers from the list add up to k The task:


  Given a list of numbers and a number k, return whether any two numbers
  from the list add up to k.
  
  For example, given [10, 15, 3, 7] and k of 17, return true since 10 +
  7 is 17.
  
  Bonus: Can you do this in one pass?


const lst = [14, 10, 3, 7, 0];
const k = 14;


My solution 1:

const addUpTo = (lst, k) =&gt; lst.some((x, i) =&gt; !isNaN(lst.find((y, j) =&gt; i !== j &amp;&amp; y === k - x)));

console.log(addUpTo(lst, k));


My solution 2:

const addUpTo2 = (lst, k) =&gt; lst.some((x, i) =&gt; {
  const find = k - x;
  return !isNaN(lst.slice(i + 1).find(y =&gt; y === find));
});

console.log(addUpTo2(lst, k));
These solutions have quadratic runtime.  You can store counts (in an object) and check the object for k-x, or sort the array, check values up to k/2 and binary search for k-x.  The second approach is asymptotically slower but constant storage.