Machine Learning Coding Problems

Consider only the range [1, K*(b-a+1)] from the values of Q. Among this range, if we divide it into b-a+1 equal parts each of size K, then for any integer in the 1st part i.e. [1, K] we return 1, for 2nd part [K+1, 2K] return 2 and so on.

Thus int((Q-1)/K)+1 will return random integer in the range [1, b-a+1]. To return in the range [a, b], add a-1 to the output i.e. a+int((Q-1)/K).

Expected number of times while loop executes is 7^P/K*(b-a+1).

Visit Now