Question

What is the time complexity of this code?

I'm unsure if it is O(log(n)) or O(n).

I think that the while loop is logn but the for loop that comes after runs the same number of times as the while loop.

string toBinary(int num) { string binary = , temp = ; while (num != 0) { temp += to_string(num%2); num /= 2; for (int i =

0 0
Add a comment Improve this question Transcribed image text
Answer #1
while loop runs for O(log(n)) times

Then size of temp becomes log(n)

So, the for loop runs for size of variable temp times.
So, for loop is also runs for O(log(n)) times.

So, total time complexity
= O(log(n)) + O(log(n))
= O(log(n)) 
Add a comment
Know the answer?
Add Answer to:
What is the time complexity of this code? I'm unsure if it is O(log(n)) or O(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
  • Which big-O expression best characterizes the worst case time complexity of the following code? public static...

    Which big-O expression best characterizes the worst case time complexity of the following code? public static int foo(int N) ( int count = 0; int i1; while (i <N) C for (int j = 1; j < N; j=j+2) { count++ i=i+2; return count; A. O(log log N) B. O(log N2) C. O(N log N) D. O(N2)

  • Using C++ please explain What is the Big-O time complexity of the following code: for (int...

    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 one: o a. O(n^2) O b. O(log n) c. O(n) O d. 0(1) What is the Big-O time complexity of the following code: for(int i=1; i<N; i*=2) { ... constant time operations... Select one: O O a. O(n^2) b. 0(1) c. O(n) d. O(log n) O What is the Big-O time complexity of the following...

  • Merge Sort: Time Complexity: O(n log(n)) #include "stdafx.h" #include <iostream> #include <time.h> #include <stdlib.h> using namespace...

    Merge Sort: Time Complexity: O(n log(n)) #include "stdafx.h" #include <iostream> #include <time.h> #include <stdlib.h> using namespace std; void combine(int *a, int low, int high, int mid) {        int i, j, k, c[100000];        i = low;        k = low;        j = mid + 1;        while (i <= mid && j <= high)        {               if (a[i] < a[j])               {                      c[k] = a[i];                      k++;                      i++;               }               else               {                     ...

  • 1). What is the complexity of the following code snippet? { for (int count2 = 0;...

    1). What is the complexity of the following code snippet? { for (int count2 = 0; count2<n; count2++) { /*some sequence of O(1) step*/ } } select one: a. O(N^2) b. O(Log N) c. O(1) d. O(N!) 2). What is the complexity of the following code snippet? for (int count = 0; count<n; count++) { printsum(count) } select one: a. We need to know the complexity of the printsum() function. b. O(Log N) c. O(1) d. O(N) e. O(N^2) 3)....

  • Prove Big O in terms of nₒ and C? There are 5 examples: class Exercise {...

    Prove Big O in terms of nₒ and C? There are 5 examples: class Exercise { public static int example1(int[] arr) { int n = arr.length, total = 0; for (int j=0; j < n; j++) // loop from 0 to n-1 total += arr[j]; return total; } public static int example2(int[] arr) { int n = arr.length, total = 0; for (int j=0; j < n; j += 2) // note the increment of 2 total += arr[j]; return...

  • The time-complexity of searching an AVL tree is in the worst case and in the average...

    The time-complexity of searching an AVL tree is in the worst case and in the average case. On), On) O(logot). O(log O ON), C(n) 0(), O(log) Question 16 2 pts The time-complexity of searching a binary search tree is in the worst case and in the average case (1), O(log) O(logn), O(log,n) On), On) 001), 001)

  • What is the time complexity of the following code segment? for (int i = 0; i<n;...

    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 time complexity of the following code segment? for (int i = 0; i<10; i++) if (a[i] != 0) sum += a[i]; What is the time complexity of the following code segment? for (int i = 0; i<n/2; i++) if (a[i] != 0) sum += a[i]; What is the time complexity of the following...

  • (10') 6. For each of the following code blocks, write the best (tightest) big-o time complexity...

    (10') 6. For each of the following code blocks, write the best (tightest) big-o time complexity i) for (int i = 0; ǐ < n/2; i++) for (int j -0: ni j++) count++ i) for (int í = 0; i < n; i++) for (int ni j0 - for (int k j k ni kt+) count++ İİİ) for (int í ー 0; i < n; i++) for(int j = n; j > 0; j--) for (int k = 0; k...

  • Please help me with this Java project. I'm trying to create a loop so if user...

    Please help me with this Java project. I'm trying to create a loop so if user enter value > 12, will prompt them a message and take them back to "How many elements do you wan to enter?". Thanks public static void main(String[] args) {        Scanner sc = new Scanner(System.in);           int i, j;       System.out.print("\n How meny elements do you want to enter? (N should be no more than 12) ");    int num...

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