Question

What is the time complexity of the following code segment? for (int i = 0; i<n; i--) if (a[i] != 0) sum = a[i]; What is the t

0 0
Add a comment Improve this question Transcribed image text
Answer #1
  1. The number of iterations of the for loop in the code segment will be equal to n. So the time complexity will be O(n).
    Answer: O(n)
  2. The number of iterations of the for loop will be 10. So the code segment will run in constant time.
    Answer: O(1)
  3. The number of iterations of the for loop will be n/2. So the code segment will run in O(n/2) time.
    Answer: O(n/2)
  4. The number of iterations of the for loop will be n/5 as i will run from 0 to n taking only the multiples of 5. So, the time complexity of the code segment will be O(n/5).
    Answer: O(n/5)
Add a comment
Know the answer?
Add Answer to:
What is the time complexity of the following code segment? for (int i = 0; i<n;...
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