The time complexity of the following code is: for (int i = n; i > 0; i /= 2) { for (int j = 1; j < n; j *= 2) { for (int k = n; k >= 1; k /= 2) { constant number of operations O((log n)^3) O(log log log n) O(n) O(n - log n) } } }
Added by Kevin P.
Step 1
The middle loop runs for log(n) iterations because j is multiplied by 2 in each iteration until it becomes greater than or equal to n. The inner loop runs for log(n) iterations because k is divided by 2 in each iteration until it becomes 1. Show more…
Show all steps
Your feedback will help us improve your experience
Ashar Tanveer and 69 other AP CS educators are ready to help you.
Ask a new question
Labs
Want to see this concept in action?
Explore this concept interactively to see how it behaves as you change inputs.
Key Concepts
Recommended Videos
Supreeta N.
Consider the following lines of code and compute the overall big O complexity: for i in range(n): if i % 3 == 0: for j in range(1000): sum += j else: for j in range(i + 1): sum += j
Time complexity of $f(x)=4 x^{2}-5 x+3$ is (A) $O(x)$ (B) $O\left(x^{2}\right)$ (B) $O\left(x^{3 / 2}\right)$ (D) $O\left(x^{0.5}\right)$
Programming and Data Structures
Asymptotic Analysis
Recommended Textbooks
Computer Science and Information Technology
Introduction to Programming Using Python
Computer Science - An Overview
18,000,000+
Students on Numerade
Trusted by students at 8,000+ universities
Watch the video solution with this free unlock.
EMAIL
PASSWORD