edited by
224 views
0 votes
0 votes

Consider the following code where $\text{A}$ is an array indexed from $0.$

function foo(A, year, n) {
    1 = 0, r = n – 1, c = 0;
    while (1 <= r) {
        c = c + 1;
        m = 1 + (r – 1) // 2;
        if (A[m] == year) {
            return(c * m);
        }
        if (A{m} < year) {
            1 = m + 1;
        } else {
            r = m – 1;
        }
    }
    return (-1);
}

function bar ()  {
    A = [2016, 2017, 2018, 2019, 2020, 2021, 2022];
    result = foo(A, 2021, 7);
    print (result);
} 

Here, $\text{a // b}$ represents integer division. For example, $23 / / 10=2$. What will be printed when $\textsf{bar()}$ is executed?

edited by

Please log in or register to answer this question.

Related questions

0 votes
0 votes
0 answers
4
admin asked Jul 23, 2022
213 views
Show that every selection of $503$ numbers from $\{1,2,3, \dots,987\}$ has two numbers with g.c.d. $1$. Recall that the g.c.d. of two positive integers $x, y$ is the larg...