0
$\begingroup$

here is a nested loop where all the variable are integers.This is another question to the thread. I understood the solution part , but stuck in the time-complexity part.

What is the time complexity of the below code and how?

def divide(dividend, divisor):
    """
    :type dividend: int
    :type divisor: int
    :rtype: int
    """
    #Time-complexity ??
    while dividend>=divisor:
        t=0;k=divisor

        while dividend>=k:
            dividend-=k
            k<<=1
            q+=1<<t
            t+=1

    return q
$\endgroup$
  • $\begingroup$ What is unnerving is that apparently you have not even tried to analyse what this code is doing. Your teacher created this code with exactly the intent that you analyse it and figure out yourself what it is doing. (That’s quite obvious from the fact that it is far from optimal). At the very least you should be able to describe the total effect and runtime of running the inner loop to its end. In the end, it’s you who is missing out. $\endgroup$ – gnasher729 Nov 2 '19 at 21:08
  • $\begingroup$ We're not looking for questions that are just the statement of an exercise-style problem. What did you try? Where did you get stuck? We're happy to help you understand the concepts but just solving exercises for you is unlikely to achieve that. You might find this page helpful in improving your question. $\endgroup$ – D.W. Nov 3 '19 at 6:04
  • $\begingroup$ @D.W., i have modified. Please check now. $\endgroup$ – tedd Nov 3 '19 at 6:09
  • $\begingroup$ Your question still says nothing at all about what you tried or where you got stuck. It's still just "Here's an exercise I was set. I can't do any of it. Please do it for me." $\endgroup$ – David Richerby Nov 3 '19 at 8:49
2
$\begingroup$

It is $O(\log^2 \frac{\text{dividend}}{\text{divisor}})$.

The inner loop clearly takes at most $O(\log \frac{\text{dividend}}{\text{divisor}})$ time since initially $k= \text{divisor}$ and it is doubled at every iteration.

The outer loop requires at most $O(\log \frac{\text{dividend}}{\text{divisor}})$ iterations since the inner loop subtracts from $\text{dividend}$ the highest multiple $x = k \cdot \text{divisor}$ such that $k=2^i - 1$ for some integer $i$ and $x \le \text{dividend}$. This is true because initially $k \le \log ( 1+ \frac{\text{dividend}}{\text{divisor}})$ and the same value of $i$ cannot be selected more than two times in a row since $3 \cdot (2^i - 1) = 3 \cdot 2^i - 3 = 2^{i+1} + (2^i-3) \ge 2^{i+1} - 1$.


This analysis is tight in the following sense: let $\text{divisor}=1$ and $S_j = \sum_{i=0}^j (2^i - 1)$. Let $\text{dividend}= S_\ell$ for some $\ell \ge 2$ of choice.

Clearly $S_j > 2^j - 1$. Moreover, we can show by induction on $j$ that $S_j < 2^{j+1}-1$.

The base case $j=0$ is trivial: $S_0 = 0 < 2^1 - 1 = 1$. Suppose now that the claim is true for $j-1$: We have $S_j = S_{j-1} + (2^j - 1) < 2^j - 1 + (2^j - 1) = 2^{j+1} - 2$.

This means that when divisor is $S_j$, the inner loop will subtract $2^{k}-1$ where $k=j$ (in $k-1$ iterations), and the divisor will become $S_{j-1}$. It follows that $\ell$ iterations of the outer loop are needed to reach $S_0 = 0$.

The overall number of iterations of the inner loop is then: $$ \sum_{i=1}^\ell (i-1) = \sum_{i=0}^{\ell-1} i = \frac{\ell (\ell-1)}{2} = \Theta(\ell^2). $$

But $\ell \ge \log S_\ell$, since $S_\ell$ has at least $\ell$ binary digits and hence the above quantity is $\Omega(\log^2 S_\ell) = \Omega(\log^2 \text{dividend}) = \Omega(\log^2 \frac{\text{dividend}}{\text{divisor}})$.

$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.