Question
c++
QUESTION 1 What is the Big-Oh of the following computation? int sum = 0; for (int counter = 1; counter <n; counter++) sum = s
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the step by step time taken by each statement

int sum = 0; This statement takes 1 unit of time
for(int counter = 1; counter < n; counter++) This statement is executed n times
So its time taken is n units
1 is added extra for a loop
because it has to check condition 1 extra time
sum = sum + counter This statement is executed n - 1 times
So its time taken is n - 1 units

Total time = 1 + n + (n - 1)
= 2 * n
= O( n )

Thus time complexity of the above program is O(n)

Hope this helps. Please rate the answer if you like it.

Add a comment
Know the answer?
Add Answer to:
c++ QUESTION 1 What is the Big-Oh of the following computation? int sum = 0; for...
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