edited by
219 views
0 votes
0 votes

Consider the following code, in which $\textsf{A}$ is an integer array of length $\textsf{n}$ indexed from $\textsf{0},$ and $\textsf{x}$ is an integer.
 

function foo (A, x, n)  {
    found = False ;

    while (found != True)  {
        i = randInt (0, n) ;

        if (A [i] == x)  
            found = True ;
        }
    }
    return (i) ;
}

Here, $\textsf{randInt (0, n)}$ returns an integer picked uniformly at random from the range $\{ 0, 1, \dots, (n-1) \}.$

All integers present in array $\textsf{A}$ are distinct, and integer $\textsf{x}$ is present in array $\textsf{A}.$ Suppose we make the call $\textsf{foo (A, x, n)}.$ What is the expected number of times that the call $\textsf{randInt (0, n)}$ is made from within this call to $\textsf{foo (A, x, n)}?$              

edited by

Please log in or register to answer this question.

Related questions

0 votes
0 votes
0 answers
1
admin asked Jul 23, 2022
209 views
At a conference attended by $1235$ people, some attendees shake hands with other attendees. As a part of the local $\text{COVID-19}$ tracing protocol the organizers ask e...