Last active
October 20, 2017 19:44
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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