I'm having a trouble with what should be a very simple algorithm, but for some reason my head is not working properly (too much work?)
I have an Array of numeric values: [ 10, 20, 30, 40, 100, 1000, 5000, 100000] I want to check which is the next "item" in the array.
For example,
- given the number 10, my algorithm should return 10.
- given the number 1, my algorithm should return 10
- given the number 50, my algorithm should return 100.
- given the number 99999999, my algorithm should return 100000
In pseudo-code I was thinking:
for previousValue, nextValue in values:
if ( previousValue < value && nextValue >= value ):
return nextValue
return values[max]
If anyone can point out to my exhausted brain what I'm missing it would be great. thanks!