Skip to content

Instantly share code, notes, and snippets.

@manivelarjunan
Created October 18, 2018 03:44
Show Gist options
  • Select an option

  • Save manivelarjunan/679aa4d470265700bafca4e84bcd2a35 to your computer and use it in GitHub Desktop.

Select an option

Save manivelarjunan/679aa4d470265700bafca4e84bcd2a35 to your computer and use it in GitHub Desktop.
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));
@dhrvmhptr

Copy link
Copy Markdown

Hey, I'm not quite able to understand the logic behind this code. Could you please explain how this works?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment