edited by
973 views
2 2 votes

The below question is based on the following program. In the program, we assume that integer division returns only the quotient. For example $7/3$ returns $2$ since $2$ is the quotient and $1$ is the remainder.

mystery(a,b){
        if (b == 0) return a;
        if (a < b) return mystery(b,a);
        if (eo(a,b) == 0){
             return(2*mystery(a/2,b/2));
    }
    if (eo(a,b) == 1){
        return(mystery(a,b/2));
    }
    if (eo(a,b) == 2){
        return(mystery(a/2,b));
    }
    if (eo(a,b) == 3){
        return (mystery((a-b)/2,b));
    }
}
eo(a,b){
if ((a/2)*2 == a and (b/2)*2 == b) return 0; end;
if ((a/2)*2 < a and (b/2)*2 == b) return 1; end;
if ((a/2)*2 == a and (b/2)*2 < b) return 2; end;
return 3;
}

When $a$ and $b$ are $n$ bit positive numbers the number of recursive calls to $\text{mystery}$ on input $a,\: b$ is

  1. $O(n)$
  2. $O(\log \log n)$
  3. $O(\log n)$
  4. $O(n^{\frac{1}{2}})$

1 Answer

1 1 vote
O(log n)
Position:
Show:

Related questions

2 2 votes
1 1 answer
856
856 views
go_editor asked May 22, 2016
856 views
The below question is based on the following program. In the program, we assume that integer division returns only the quotient. For example $7/3$ returns $2$ since $2$ i...
1 1 vote
1 1 answer
1.4k
1.4k views
go_editor asked May 23, 2016
1,433 views
You have an array $A$ with $n$ objects, some of which are identical. You can check if two objects are equal but you cannot compare them in any other way — i.e., you can c...
4 4 votes
1 1 answer
2.4k
2.4k views
go_editor asked May 22, 2016
2,405 views
You are given two sorting algorithms A and B that work in time $O(n \log n)$ and $O(n^2)$, respectively. Consider the following statements:Algorithm $A$ will sort any arr...
7 7 votes
1 answers 1 answer
1.9k
1.9k views
go_editor asked May 27, 2016
1,874 views
Given an undirected weighted graph $G = (V, E)$ with non-negative edge weights, we can compute a minimum cost spanning tree $T = (V, E')$. We can also compute, for a give...