• edited by
39,987 views
143 143 votes

The cube root of a natural number $n$ is defined as the largest natural number $m$ such that $(m^3 \leq n)$ . The complexity of computing the cube root of $n$ ($n$ is represented by binary notation) is 

  1. $O(n)$ but not $O(n^{0.5})$
  2. $O(n^{0.5})$ but not $O((\log n)^k)$ for any constant $k>0$
  3. $O((\log n)^k)$ for some constant $k>0$, but not $O( (\log \log n)^m)$ for any constant $m>0$
  4. $O( (\log \log n)^k )$ for some constant $k > 0.5$, but not $O( (\log \log n)^{0.5} )$

10 Answers

Best answer
145 145 votes
We can simply do a binary search in the array of natural numbers from $1..n$ and check if the cube of the number matches $n$ (i.e., check if $a[i] * a[i] * a[i] == n$). This check takes $O(\log n)$ time and in the worst case we need to do the search $O(\log n)$ times. So, in this way we can find the cube root in $O(\log^2 n)$. So, options (A) and (B) are wrong.

Now, a number is represented in binary using $\log n$ bit. Since each bit is important in finding the cube root, any cube root finding algorithm must examine each bit at least once. This ensures that complexity of cube root finding algorithm cannot be lower than $\log n$. (It must be $\Omega \left( \log n \right)$). So,  (D) is also false and (C) is the correct answer.
• edited by
79 79 votes

Consider the below function:

int cuberoot(int n)
{
    int crt;
    for(int i =1; i<=n;i++)
    {
    if (i*i*i<=n)  crt=i;
    else return(crt);
    }
}

The above function will return the cube root of value 'n' as defined in question.

Now if n = 64, the for loop will run 5 times(i=1,2,3,4,5) O(logn).

But if we take larger value say n= 2^30 then the for loop will run (2^10 +1=1025)  times (since 2^10 * 2^10 * 2^10 =2^30) which is  (logn)^k , where k=2.038.

Therefore we can say that the complexity of computing cuberoot  will O((logn)^k) but not O(loglogn).

Hence Answer is (C)

• edited by
1 flag:
✌ Edit necessary (Anurag Prasad “Incorrect solution. The code shown in answer is O(n^1/3) and not O(logn) as claimed.”)
17 17 votes

EDITED:-

See, if we have all the numbers in a sorted fashion in an array, we can use binary search which takes $O(logn)$ time.

So, the time complexities mentioned in Options A and B hold. The "but not" part makes these options wrong.

The question says that 

n is represented by binary notation

Hence the size of each number is $O(logn)$.

When performing the binary search, all the $O(logn)$ bits of a number would be potentially checked to find a match. Hence, at the minimum this algorithm would take $O(logn*logn)$ time = $O(logn)^2$

Since this would be the minimum time, we can't get $O(loglogn)$, hence Option D is incorrect, too.

 

Option C

• edited by
16 16 votes

Option C

Using mcLaurin series

1 1 vote

Here we  need to find upper and perhaps lower bounds on the complexity of finding an integer cube root m of n. At least one upper bound is trivial, and rules out answers A and B: m can be found in O(log n) time using binary search.

Also note that the input size is O(log n) because the minimum number of bits needed to represent an arbitrary n in binary notation is proportional to log n. Because all bits of the number must be processed to solve the problem, θ(log n) is a lower bound on the time to solve the problem, and therefore the problem cannot be solved in time O((log log n)^w) [where w is some constant > 0] because that isn't O(log n). Thus, answer C applies.

Source:http://www.techtud.com/doubt/gate-2003

1 1 vote

The problem asks for the complexity of computing the cube root of n, where n is represented in binary. This means the size of the input is not the value n, but the number of bits required to represent n, which is b=Θ(logn).

The most efficient algorithm for this is to use binary search.

  1. The Algorithm: We are looking for the largest integer m such that m3≤n. We can binary search for this value m in the range [1,n].

  2. Number of Iterations: A binary search on a range of size n takes O(logn) iterations.

  3. Cost of Each Iteration: In each iteration, we take a midpoint mid and compute mid³.

    • The numbers we are dealing with (like mid) can have up to Θ(logn) bits.

    • The cost of multiplying two b-bit numbers is, at worst, O(b2). So, computing mid³ takes O((logn)2) time.

  4. Total Time Complexity:

    Total Time=(Number of Iterations)×(Cost per Iteration)

    Total Time=O(logn)×O((logn)2)=O((logn)3)

Evaluating the Options

Our calculated complexity is O((logn)3). Let's see which option this fits.

  • A. O(n)...: False. O((logn)3) is much smaller than O(n).

  • B. O(n0.5)...: False. O((logn)3) is much smaller than O(n0.5).

  • C. O((logn)k) for some constant k>0, but not O((loglogn)m) for any constant m>0:

    • Is our complexity O((logn)k)? Yes, for k=3.

    • Is it "not O((loglogn)m)"? Yes, because (logn)3 grows faster than any power of loglogn.

    • This statement is true.

  • D. O((loglogn)k)...: False.

This confirms that C is the correct answer. The key is to analyze the complexity in terms of the number of bits, which leads to a polylogarithmic time complexity.

 

Let's run the algorithm on two examples: a non-perfect cube () and a perfect cube ().

The goal is to find the largest integer m such that .


 

Example 1: n = 30

 

Initial state: low = 1, high = 30, answer = 0

Iterationlowhighmidmid³Condition mid³ <= 30?Actionanswer
1130153375Falsehigh = 140
21147343Falsehigh = 60
316327Trueanswer = 3, low = 43
4465125Falsehigh = 43
544464Falsehigh = 33

At this point, low is 4 and high is 3. The condition low <= high is false, so the loop terminates.

The final answer is the last stored value in answer, which is 3. This is correct, as 3^3=27≤30 and 4^3=64>30.

 


 

Example 2: n = 27

 

Initial state: low = 1, high = 27, answer = 0

Iterationlowhighmidmid³Condition mid³ <= 27?Actionanswer
1127142744Falsehigh = 130
21137343Falsehigh = 60
316327Trueanswer = 3, low = 43
4465125Falsehigh = 43
544464Falsehigh = 33

Again, the loop terminates because low (4) becomes greater than high (3).

The final answer is 3, which is the correct integer cube root of 27.

Answer:
Position:
Show:

Related questions

61 61 votes
5 answers 5 answers
14.8k
14.8k views
Kathleen asked Sep 17, 2014
14,846 views
A program consists of two modules executed sequentially. Let $f_1(t)$ and $f_2(t)$ respectively denote the probability density functions of time taken to execute the two ...
86 86 votes
3 answers 3 answers
27.2k
27.2k views
Kathleen asked Sep 16, 2014
27,200 views
The usual $\Theta(n^2)$ implementation of Insertion Sort to sort an array uses linear search to identify the position where an element is to be inserted into the already ...
78 78 votes
8 answers 8 answers
25.8k
25.8k views
Kathleen asked Sep 16, 2014
25,753 views
Consider the following recurrence relation$T(1)=1$$T(n+1) = T(n)+\lfloor \sqrt{n+1} \rfloor$ for all $n \geq 1$The value of $T(m^2)$ for $m \geq 1$ is$\frac{m}{6}\left(21...
94 94 votes
11 answers 11 answers
31.8k
31.8k views
go_editor asked Apr 24, 2016
31,841 views
In a permutation $a_1\ldots a_n$, of $n$ distinct integers, an inversion is a pair $(a_i, a_j)$ such that $i < j$ and $a_i a_j.$What would be the worst case time complex...