Question: The following function divides one integer by another by the rather dubious process of making a series of random guesses until it happens to guess

The following function divides one integer by another by the rather dubious process of making a series of random guesses until it happens to guess correctly.
int idiv(int x, int y){
// Compute x / y
if (x <=0)
return x;
int k = rand.nextInt(x+1);
while (!(k*y <= x && (k+1)*y > x))
k = rand.nextInt(x+1);
return k;
}
Assume that rand.nextInt(n), each time that it is called, returns a randomly selected integer in the range 0..n-1, inclusive, with every integer in that range being equally likely to occur on any given call, and that each nextInt(n) call runs in O(1) time.
What is the average-case complexity of this function?
Group of answer choices
O(1)
O(x)
O(y)
O(n)
O(x / y)
O(log x)
O(log y)
None of the given answers are correct.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!