Skip to content
Advertisement

Find the smallest number that is greater than a given number in a sorted list

Given a sorted list of numbers, I need to find the smallest number that is greater than a given number. Consider this list:


JavaScript

Say the specified number is 320. Then, my method should return 353 as 353 is the smallest number greater than 320.

I am trying to use a slightly modified form of binary search; however on execution the program goes into infinite loop.


JavaScript

Can someone point out what I am doing wrong ?

Advertisement

Answer

If arr[mid] and arr[mid-1], both are greater than your number, you should search in arr[0:mid], don’t you think?

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement