It must be Θlog*n*

I believe you mean Ω(log n)

EDIT>> Ah! My bad.. you showed it is already O (log n) above.. fine..

The Gateway to Computer Science Excellence

First time here? Checkout the FAQ!

x

+45 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

- $O(n)$ but not $O(n^{0.5})$
- $O(n^{0.5})$ but not $O((\log n)^k)$ for any constant $k>0$
- $O((\log n)^k)$ for some constant $k>0$, but not $O( (\log \log n)^m)$ for any constant $m>0$
- $O( (\log \log n)^k )$ for some constant $k > 0.5$, but not $O( (\log \log n)^{0.5} )$

+59 votes

Best answer

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.

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.

0

*n*

I believe you mean Ω(log n)

EDIT>> Ah! My bad.. you showed it is already O (log n) above.. fine..

+2

Can you make it more simple ?

What do you ment by this ?

(a[i] * a[i] * a[i] == n).

How it appears to be O( log n ) O(logn)

+2

@Arjun Not understood first paragraph. Why are we doing binary search and of which particular number. We are multiplying cube root of n three times. But from where did we know that this is cube root or not ?

+5

I think since the 1...n are sorted so if we do binary search and if a[i]*a[i]*a[i] is greater than n then we can surely say that cube root is in left side of array and if cube root is less than n then in right side array...

+5

yes. Normally we do $a[i] == x$ in binary search. Here we must do $a[i] \times a[i] \times a[i] == n$. One more thing - if search fails - that is no prefect cube root exist, then the last number being compared should be returned.

0

Arjun Sir, can you elaborate me about the solution clearly. I can't understand why it is necessary to use binary search instead of linear search

+2

If we use linear search complexity will be $O\left(n^{\frac{1}{3}}\right)$ since we have do do sequential checking from 1 to $n^{\frac{1}{3}}$.

+1

" a number is represented in binary using logn bit. Since each bit is important in finding the cube root, any cube root finding algorithm must examine each bit at least once."

How does each bit is important in finding cube root? please explain

How does each bit is important in finding cube root? please explain

+1

Change any bit of a number from 0 to 1 or vice versa. Its cube root also changes. So, every bit is significant in finding the cube root.

+2

@gatecse. check if a[i]∗a[i]∗a[i]==n). This check takes O(logn) time

how does this check takes O(logn) time??

0

what if the number n is converted to decimal to find cube root and then find its complexity.will it be wrong to convert the number to decimal?

0

"We can simply do a binary search in the array of natural numbers from 1..n"

@Arjun sir from where this array comes?

@Arjun sir from where this array comes?

+1

@Ahwan There's no need to store the numbers in array. He just means perform a binary search on the range [1, n] for the cube root of n. This is because the cube root of n will always be in the range [1,n]. http://ideone.com/i6lWc6

0

"..in the worst case we need to do the search O(logn) times".

I am not getting this. In O(logn) time, we get the i for which i^3<=n using binary search. What will be worst case? Please help

I am not getting this. In O(logn) time, we get the i for which i^3<=n using binary search. What will be worst case? Please help

+2

@sks24 It's given that the number n is given in binary, so there are $logn$ bits, naive multiplication will be done in $log^2n$, then $logn$ for the binary search, so I think final complexity should be $O(log^3n)$

EDIT: If we convert the binary to decimal in the beginning, then it will be $O(logn)$

EDIT: If we convert the binary to decimal in the beginning, then it will be $O(logn)$

+3

@Arjun sir. How is it logn^2. It should be logn only? 1...n we do binary search and each time we check a(i)*a(i)*a(i)=n or not and this is a constant time and it will be done logn times at max.Can you tell please how is it logn^2?

0

@air1

I think final complexity should be O(log3n)O(log3n)

EDIT: If we convert the binary to decimal in the beginning, then it will be O(logn)

Please explain how it becomes O(logn)?

I think final complexity should be O(log3n)O(log3n)

EDIT: If we convert the binary to decimal in the beginning, then it will be O(logn)

Please explain how it becomes O(logn)?

+15

a == b? Usually we consider this as an $O(1)$ operation. But when $a$ is allowed to be arbitrarily large, this can take $O(\log a)$ as each bit needs to be checked. That is why the check is taking $O(\log n)$ in the given question.

+1

Sir in asymptotic analysis we dont bother about how long arithmetic operations take.It depends on h/w .

even when we check a==b in constant as you mentioned,there also a can be any large or small number and we need to check all bit,but we dont bother about that.I still did not get:(

even when we check a==b in constant as you mentioned,there also a can be any large or small number and we need to check all bit,but we dont bother about that.I still did not get:(

+1

@Arjun sir, why do the binary search on the whole of n elements? Isn't it sufficient to do it only for the 1st n^1/3 elements? If so, then the time taken is also reduced to O(log n^1/3).

0

cant we do like this?

1) convert given binary representation of $n$ into decimal----$O(logn)$

2) then find $\left \lfloor n^{0.333} \right \rfloor$ --------------$O(1)$

total complexity $O(logn)$

@Arjun sir I have same doubt as above @rahul said.

Do we consider the underlying hardware complexity in asymptotic analysis of our algorithm?

+11 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)**

0 votes

Consider the array of natural numbers .

00 | 01 | 10 | 11 | 100 | 101 | 110 |

Suppose we want to find the cube root of $ 6$ , $(n=6)$ to store in binary it takes 3-bits ($ \left \lceil \log_2 n \right \rceil$)

take $m$ as middle element of the array. (here $m=3$)

step 1 : Find middle element $m$ [ will take $O(1)$ time ]

step 2 : Find cube of m [ will take $O(1)$ time ]

step 3 : Compare $m^{3} < n$ [ will take $\left \lceil \log_2 n \right \rceil$ time . We need to compare $\left \lceil \log_2 n \right \rceil$ bits because of binary representation ]

step 4 : If there is match or nothing in array is left we return m else if compared value is grater than $m$ we go left of $m$ else we go right of $m$ loop from step 1

Running Time Complexity Of the Algorithm

- $\log n$ -> Implementing Binary Search
- $( \log n )^{k}$ -> Comparing Each bit of the number represented in binary notation where k is the number of loop iterations
- So on best case it will take $O(\log n)$ and worst case $O(\log^{k+1} n)$

**Example**

Iteration 1

- $m=3$
- $3^{3}=9$
- $9 \leq 6$ False we go LEFT of the array [this is compared as $1001\leq 110 $]

Iteration 2

- $m=1$
- $1^{3}=1$
- $1\leq 6$ True we go RIGHT of the array [this is compared as $01\leq 110 $]

Iteration 3

- $m=2$
- $2^{3}=8$
- $8\leq 6$ False we go LEFT of the array [this is compared as $1000\leq 110 $]
- Since sonthing in LEFT of the array we return $2$ as it is the nearest cube root

Here we took $O(\log ^{4} n)$

[ @arjun sir can u pls check this one ]

0

@arjun sir ,

Actually What i Thought about upperbound complexity $O(\log^{k+1} n)$ is that

- Outer Loop for implementing binary search will take $O( \log n)$ time
- inner loop for comparing each bit of $n$ for each iteration takes $O( \log n)$ each

Isn't the example correct ?

- $O(\log n) $ for implementing binary search
- $O(\log n) $ for comparing bits of 9 and 6
- $O(\log n) $ for comparing bits of 1 and 6
- $O(\log n) $ for comparing bits of 8 and 6

Here k = number of iterarions . The loop executes for $\log n $ time hence $k=\log n$

Correct me if wrong...

+1

Got my mistake :)

This is wrong .

@arjun sir complexity will be always $O(\log^{2} n)$ ?

How does k counted ? will it affect the complexity ?

This is wrong .

@arjun sir complexity will be always $O(\log^{2} n)$ ?

How does k counted ? will it affect the complexity ?

+4

yes, though I did not prove it. Complexity of multiplying 3 numbers of $\log n$ bits each. Normally we take this as $O(1)$ assuming CPU has a MUL instruction. For this question, we can assume such an instruction is not there.

0 votes

BY USING LINEAR SEARCH:

algo(n)

{ for(i=1 to n^3)

if(i^3>n) return(i-1)

}

it takes** n^(1/3) **time

BY USING BINARY SEARCH:

(**1**)^3 (**2**)^3 3 (**4**)^3 5 6 7 (**8**)^3 9 10 11 12 13 14 15 (**16**)^3 ..........

it takes** logn** time

so** c** is answer

0 votes

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.

- All categories
- General Aptitude 1.6k
- Engineering Mathematics 7.5k
- Digital Logic 3k
- Programming & DS 4.9k
- Algorithms 4.3k
- Theory of Computation 6k
- Compiler Design 2.1k
- Databases 4.2k
- CO & Architecture 3.5k
- Computer Networks 4.2k
- Non GATE 1.4k
- Others 1.5k
- Admissions 586
- Exam Queries 572
- Tier 1 Placement Questions 23
- Job Queries 72
- Projects 18

50,132 questions

53,252 answers

184,792 comments

70,509 users