I don't know how to make this more efficient. My answers seem to be correct, but I get a "Time Limit Exceeded" error all the time. Can anyone help me with this? Thanks.
This will almost certainly be false, because doubles are stored as binary fractions and cannot represent every real number. This means there is no exit from the infinite for loop - hence the time limit problem.
To fix this, you need an arbitrary precision value such as :
double MyPrecision = 0.001;
if amount is less than MyPrecision then that is the end condition. Your break only breaks from the for loop that it is in, so you need a better way. Try a while loop instead.
Or you may read the integer value with an integer variable.
Still, you need to analyse the input limits
Suppose as input 2^{31} 0
the answer is obviously `1', but your first loop will go all the way from 2^{31} to 0 (waste of time)
Then you will realize that double is not precise. Check out Tartaglia's triangle.
The order will be O(n), where n=min(small, diff) (however n<= 2^{16}, prove it)