bideshbanerjee444's blog

By bideshbanerjee444, history, 3 years ago, In English

for(int i = 2; i <= int(1e6); i++){
for(int j = 0; j <= int(1e6); j += i){

//Here anything which takes O(1) time.....

} }

  • Vote: I like it
  • -18
  • Vote: I do not like it

| Write comment?
»
3 years ago, # |
Rev. 2   Vote: I like it +5 Vote: I do not like it

Complexity is O(nlogn), so it will pass.
Also, you can use custom invocation to check run time.

  • »
    »
    3 years ago, # ^ |
      Vote: I like it -33 Vote: I do not like it

    wrong is O(1)

    • »
      »
      »
      3 years ago, # ^ |
        Vote: I like it +9 Vote: I do not like it

      Notice that j += i. So the overall complexity is $$$O(\frac{n}{1} + \frac{n}{2} + \dots + \frac{n}{n}) = O(n \log n)$$$

      I dont think computer are smart enough to solve that kind of loop.

      • »
        »
        »
        »
        3 years ago, # ^ |
          Vote: I like it -36 Vote: I do not like it

        but n is constant (1e6)

        • »
          »
          »
          »
          »
          3 years ago, # ^ |
            Vote: I like it +3 Vote: I do not like it

          So?

        • »
          »
          »
          »
          »
          3 years ago, # ^ |
            Vote: I like it -8 Vote: I do not like it

          It is more complex then you think just a $$$O(1)$$$ and compiler can predict all 2-loops.

          Some problem are even already hard to make $$$O(n \log n) \rightarrow O(n \log \log n)$$$ even if the inside function run in $$$O(1)$$$

          • »
            »
            »
            »
            »
            »
            3 years ago, # ^ |
              Vote: I like it +29 Vote: I do not like it

            he is trying to make joke about O(any constant) = O(1), and 10^6 log 10^6 is constant here

            • »
              »
              »
              »
              »
              »
              »
              3 years ago, # ^ |
                Vote: I like it 0 Vote: I do not like it

              Ok I get the joke. And well it is also true as there is the fact that the loop $$$10^6$$$ parts are not dependent on a variable taken from the input.