Question

Compute the Big O notation. Explain how you got the answer.

on W NA 1 public String modify (String str) { if (str.length() <= 1) return ; int half = str.length() / 2; modify(str.subst1 2 3 for (int i = 0; i<n; i++) { for (int j 0; j < 5; j++) { for (int k = 0; k<n; k++) { 4 if ((i != j) && (i != k)) { 5 Sys1 int i, j, k, x = 0; 2 for (i = n; i > 0; i--) { 3 k = i *n; 4 for (j k; j<n; j++) { 5 x = x + j; 6 } 7 i = i k; 8 }

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

1. O(logN)(base 2) because everytime we are reducing the size of the problem by half i.e. with a factor of 2.

2. First loop runs from i = 0 to n, therefore, its complexity is O(N).

Second loop runs from j = 0 to 5, i.e for some constant time, its complexity is O(5).

The third loop again runs from 0 to N for k, complexity being O(N).

Since these are the nested for loops, we get the complexity of snippet to be O(5N^2), here 5 is a constant, ignore this, complexity is O(N^2).

3. First loop runs from n to 0, again it is O(N).

Inside the first loop, we are changing k as i*N, in the following patter, N^2, (N-1)N, (N-2)N, .... 1(N).

The inner loop runs from j = K but the condition being j < N, and we clearly from the above pattern for k see that it can never be less tha 0, hence this loop never executes.

The overall time complexity is thus O(N) because of the outside loop.

Add a comment
Know the answer?
Add Answer to:
Compute the Big O notation. Explain how you got the answer. on W NA 1 public...
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
  • b. what is the order (big -o) of this algorithm? 11. To answer this question, consider...

    b. what is the order (big -o) of this algorithm? 11. To answer this question, consider the n, consider the following algorithm: for (int i-0; i<ni i++) for (int j = 0; j <= i; j++) // three assignment statements in body of this inner loop a. (6 pts) Exactly how many assignments (in terms of n) are made in this algorithm?

  • 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...

  • Big-O notation for each public static double accumulate (double[] a) { double sum = 0.0; for...

    Big-O notation for each public static double accumulate (double[] a) { double sum = 0.0; for (int i = 0; i < a.length; i++) sum += a[i]; return sum; } public static double innerProduct(double[] a, double[] b) { // assume a. length == b.length double sum = 0; for (int i = 0; i < a.length; i++) sum += a[i] * b[i]; return sum; } public static int twoSum(int[] a) { int count = 0; for (int i = 0;...

  • please help, no explanation just need answer #include <stdio.h> #include <stdlib.h> int main(void) { int -x5);...

    please help, no explanation just need answer #include <stdio.h> #include <stdlib.h> int main(void) { int -x5); for (int = 0; i < 5; i++) { x[i] = malloc(sizeof (int) - 5); for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { x[i][j] = i j ; modify(x, 5, 5); return 0; Which of the implementations of method modify below set all elements of the matrix x to zero? 1. void...

  • Describe the worst case running time of the following pseudocode functions in Big-Oh notation in terms...

    Describe the worst case running time of the following pseudocode functions in Big-Oh notation in terms of the variable n. Show your work b) void func(int n) { for (int i = 0; i < n; i = i + 10) { for (int j = 0; j < i; ++i) { System.out.println("i = " + i); System.out.println("j = " + j);

  • Please answer both questions thank you. Question 52 (1 point) What is the output of the...

    Please answer both questions thank you. Question 52 (1 point) What is the output of the code snippet given below? String s = "abcde"; int i = 1; while (i < 5) { System.out.print(s.substring(i, i + 1)); i++; Question 50 (1 point) How many times will the output line be printed in the following code snippet? Please enter your answer as an integer. for (int i = 0; i < 3; i++) { for (int j = 1; j <...

  • QUESTION 5 What is the worst-case complexity of line 10 of function bar? A. O(1) B....

    QUESTION 5 What is the worst-case complexity of line 10 of function bar? A. O(1) B. O(N) C. O(i) D. O(log N) E. O(sqrt N) F. O(A[i]) G. O(N sqrt N) H. O(N log N) I. O(N^2) J. O(i^2) K. None of the above QUESTION 6 What is the worst-case complexity of lines 8-11 of function bar? A. O(1) B. O(N) C. O(i) D. O(log N) E. O(sqrt N) F. O(A[i]) G. O(N sqrt N) H. O(N log N) I....

  • QUESTION 8 What is the worst-case complexity of line 7 of function bar? A. O(1) B....

    QUESTION 8 What is the worst-case complexity of line 7 of function bar? A. O(1) B. O(N) C. O(i) D. O(log N) E. O(sqrt N) F. O(A[i]) G. O(N sqrt N) H. O(N log N) I. O(N^2) J. O(i^2) K. None of the above QUESTION 9 What is the worst-case complexity of lines 6-11 of function bar? A. O(1) B. O(N) C. O(i) D. O(log N) E. O(sqrt N) F. O(A[i]) G. O(N sqrt N) H. O(N log N) I....

  • 1.4.6 Give the order of growth (as a function of n) of the running times of...

    1.4.6 Give the order of growth (as a function of n) of the running times of each of the following code fragments: a, int sum=0; for (int k n: k > 0; k /= 2) for (int i 0; ǐ < k; İ++) sum++; b.int sum 0; for (int i = 1; i < n; i *= 2) for (int j = 0; j < i; j++) sum++; int sum = 0; for (int í = 1; i < n;...

  • Please answer both questions thank you. Question 39 (1 point) Translate the following conditions to Java,...

    Please answer both questions thank you. Question 39 (1 point) Translate the following conditions to Java, where x is an integer and s is a string. 1. x >= 6 2. x < 0 s is "Day" 3. S = "Day" x is at least 6 4. X == -7 DODO x is at most 6 5. x = -7 x is - 7 6. s.equals("Day") 7. x <= 6 x is not 27 8. X >O x is positive...

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