Skip to content
Advertisement

Tag: binary-search

Search Insert Position-Wrong answe

I’m solving a problem (leetcode 35). My code was accepted in run code result but when I submit it returns wrong answer.I don’t really understand what is wrong in my answer . Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were

Improve speed of my binary search algorithm

I have written a binary search algorithm in JavaScript: I wanted to ask if I can improve this algorithm to search faster or if some mistake is made here? EDIT: thank you guys for your help, this solution should work correctly now: Answer You are taking values as indices. If you take greater values than indices, you see your codes

Binary Search in Javascript

I’m trying to implement a binary search algorithm in JavaScript. Things seem okay, but my return statements appear to be returning undefined. Can anybody tell me what’s wrong here? Fiddle: http://jsfiddle.net/2mBdL/ Answer You’re not explicitly returning the recursive inner calls (i.e. return binarySearch()), so the call stack unfolds with no return value. Update your code like so: See a working

Advertisement