Skip to content

Instantly share code, notes, and snippets.

@techsin
Last active October 20, 2017 19:44
Show Gist options
  • Save techsin/dbd6e6eab840aceec3e1ba1d0c33e851 to your computer and use it in GitHub Desktop.
Save techsin/dbd6e6eab840aceec3e1ba1d0c33e851 to your computer and use it in GitHub Desktop.
Find if there exists two number in array that have sum equal to target.
function isThere(arr, t) {
//Usually Mergesort n log(n)
arr.sort((a,b)=> a>b?true:false);
let i = 0,
j = arr.length-1,
x = arr[i],
y = arr[j];
// O(n)
while( x + y !== t) {
( x + y > t) ? j-- : i++;
x = arr[i], y = arr[j];
if ( i >= j) return false;
}
console.log(x,y);
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment