Finding closest number in an array

前端 未结 11 1108
萌比男神i
萌比男神i 2021-01-31 22:10

In an array first we have to find whether a desired number exists in that or not? If not then how will I find nearer number to the given desired number in Java?

11条回答
  •  感动是毒
    2021-01-31 22:45

    If the array is sorted, then do a modified binary search. Basically if you do not find the number, then at the end of search return the lower bound.

提交回复
热议问题