Created
October 18, 2018 03:44
-
-
Save manivelarjunan/679aa4d470265700bafca4e84bcd2a35 to your computer and use it in GitHub Desktop.
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
| var ascendingOrder = function(a,b){ | |
| return a - b; | |
| }, | |
| BinarySearch = function(array,value){ | |
| let start = 0; | |
| let stop = array.length - 1; | |
| let middle = Math.floor((start + stop)/2); | |
| while(array[middle] !== value && start < stop){ | |
| if(value < array[middle]){ | |
| stop = middle - 1; | |
| }else { | |
| start = middle + 1; | |
| } | |
| middle = ((start + stop)/2); | |
| } | |
| return array[middle] === value ? middle : -1; | |
| }; | |
| function sumOfTwoNum(array,target){ | |
| array.sort(ascendingOrder); | |
| for(let index = 0 ; index < array.length; index++){ | |
| let isExist = BinarySearch(array.slice(index), target - array[index]); | |
| if(isExist !== index && isExist > -1){ | |
| return true; | |
| } | |
| } | |
| return false; | |
| }; | |
| console.log(sumOfTwoNum([10,15,3,3],6)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey, I'm not quite able to understand the logic behind this code. Could you please explain how this works?