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