Question

Using C++ please explain

What is the Big-O time complexity of the following code: for (int i=0; i<N; i+=2) { ... constant time operations... Select on

What is the Big-O time complexity of the following code: for(int i=1; i<N; i*=2) { ... constant time operations... Select one

What is the Big-O time complexity of the following code: int x = 10; for(int i=1; i<N; j*=2) { for(int j=0; j<N; j+=2) { X++;

0 0
Add a comment Improve this question Transcribed image text
Answer #1

for(int i=0;i<N;i+=2)
{
//constant time operations

}
Big O complexity :O(N)
explanation:
since i is incremented by 2, in each iteration
the loop run for N/2
which makes complexity :O(N/2)

for(int i=1;i<N;i*=2)
{
//constant time operations

}
Big O complexity :O(logN)
explanation:
since i is doubled, in each iteration
the loop run for logN times
which makes complexity :O(logN)


int x=10;
for(int i=1;i<N;i*=2)
{
for(int j=0;j<N;j+=2)
{
x++;
}
}
Big O complexity :O(NLogN)
explanation:
outer loop runs for LogN times
inner loop runs for N/2 times for each iteration of outer loop
overall complexity : LogN * N/2 = O(NlogN)

Add a comment
Know the answer?
Add Answer to:
Using C++ please explain What is the Big-O time complexity of the following code: for (int...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT