You are not logged in. Please login at www.codechef.com to post your questions!

×

time complexity of a loop with conditions in it !!

hi all...im using a nested loop in which i go into second loop if and only if it pass through a condition else do nothing...similarly the same in d second loop....if i run my both loops from i=1 to i=n what would be d time complexity ??

asked 06 Sep '17, 17:10

msd_007's gravatar image

1★msd_007
3178
accept rate: 5%


Depends. It can be anything from $O(NLog(LogN))$ to $O({N}^{2})$. It depends on condition and value of increment. Sieve of erastothenes is nested loop, but it runs in $O(NLog(LogN))$ (due to increasing increment after every iteration+Decreasing chances of condition being true).

But on the other hand, any thing like "If number is even then run from i=1 to N, incrementing i by 1" will be clearly $O({N}^{2})$.

link

answered 06 Sep '17, 19:04

vijju123's gravatar image

5★vijju123 ♦♦
15.4k12066
accept rate: 18%

edited 06 Sep '17, 19:05

toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×204
×83

question asked: 06 Sep '17, 17:10

question was seen: 389 times

last updated: 06 Sep '17, 19:05