Question

c++ What is the worst-case complexity of the following code fragment: for(i = 0; i <...

c++

What is the worst-case complexity of the following code fragment:

for(i = 0; i < N; i++)
{
     for(j = 0; j < N; j++)
     {
          sequence of constant time statements
     }
}

for(i = N; i > 0; i--)
{
     sequence of constant time statements
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1
// Number of values for i is N
for(i = 0; i < N; i++)
{
     // Number of values for j is N
     for(j = 0; j < N; j++)
     {
          sequence of constant time statements
     }
}

As above two loops are nested
Time complexity 
= O(Product of number of values for loop variable)
= O(N^2)


// Number of values for i is N
for(i = N; i > 0; i--)
{
     sequence of constant time statements
}

This loop is parallel to above nested loop.
We need to add N to above time complexity

So, Total time complexity of above code is
= O() + O(N)

= O() 




Add a comment
Know the answer?
Add Answer to:
c++ What is the worst-case complexity of the following code fragment: for(i = 0; i <...
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)

  • Show your work Count the number of operations and the big-O time complexity in the worst-case...

    Show your work Count the number of operations and the big-O time complexity in the worst-case and best-case for the following code int small for ( i n t i = 0 ; i < n ; i ++) { i f ( a [ i ] < a [ 0 ] ) { small = a [ i ] ; } } Show Work Calculate the Big-O time complexity for the following code and explain your answer by showing...

  • Find the best case, worst case and average case complexity for numbers of comparison and assignment...

    Find the best case, worst case and average case complexity for numbers of comparison and assignment operations for the following code. Indicate when there is no best or worst case. Comparisons Assignments Given 2-D array of integer map[n][n]: Best: Best: worst: worst: for (i0; 1 <n; i++) for(j = 0j <n; j++) If (map 10] < 0) map[001-mapli01: average: average: For ease of analysis, assume half of the elements in map are negative.

  • Exercise 7.3.5: Worst-case time complexity - mystery algorithm. The algorithm below makes some changes to an...

    Exercise 7.3.5: Worst-case time complexity - mystery algorithm. The algorithm below makes some changes to an input sequence of numbers. MysteryAlgorithm Input: a1, a2....,an n, the length of the sequence. p, a number Output: ?? i != 1 j:=n While (i < j) While (i <j and a < p) i:= i + 1 End-while While (i <j and a 2 p) j:=j-1 End-while If (i < j), swap a, and a End-while Return( aj, a2,...,an) (a) Describe in English...

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

  • Given the following code find the worst case time complexity binary search (target: integer, a[1..n ]:...

    Given the following code find the worst case time complexity binary search (target: integer, a[1..n ]: ascending integers) k =1 j =n loop when (k is less than j) m =floor((k+j)/2) if (target is larger than the element at m) then k = m+1 else j = m endloop if (target equals element at k) then location=k else location =0

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

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

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

  • Q-1: Given the following code fragment, what is its Big-O running time? test = 0 for...

    Q-1: Given the following code fragment, what is its Big-O running time? test = 0 for i in range(n):             for j in range(n):                         test= test + i *j Q-2: Given3 the following code fragment what is its Big-O running time? for i in range(n):             test=test+1 for j in range(n):             test= test - 2 Q-3: Given the following code fragment what is its Big-O running time? i = n while i > 0:             k=2+2             i...

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