Example: I have an array like this: [0,22,56,74,89] , and I want to find the closest number down to another number. Let's say that the number is 72 , and in this case the closest number is down in the array 56 , so we return this. If the number is 100 , then it is greater than the largest number in the array, so we return the largest number. If the number 22 , then this is an exact match, just return it. This number can never be under 0, and the array is always sorted.
I saw this question , but it returns the closest number depending on which is closer either up or down. I should have the one closest down, no matter what.
How do i get started? What logic should I use?
Preferably, if there are not too many cycles, as my code runs every second, and this processor is already quite intense.
source share