Question

In: Computer Science

What is the time complexity of the following code? (in big-O notaion) sum = 0 var...

What is the time complexity of the following code? (in big-O notaion)

sum = 0

var = 0

product = 0

for i = 1 to (n+2){

sum = sum + 2i

for j = i to [(n*n)+i]{

var = sum + var

for k = sum + var{

product = var++

}

}

}

for m + 1 to (j*i){

sum = sum + product

}

Solutions

Expert Solution

Big O notation is a basically mathematical notation by which we can show or see analysis of run time of a code or program in processor and memory of system.

in other word we can say that in Big O notation O stand for growth of an algorithm. Big O notation describes the limiting behavior of a function also the big O notation defines an upper bound of an algorithm for example lets take time whole time complexity of an algoritm like:

O(1) + (n)

so in big O notation we take only O(n) because O(n) is greater than O(1). this is the meaning of Big O notation or we can say it is arthematic rule of Big O notaion.

function of Big O notation is writing as

O(f(x))

where f(x) = function of time complexity of program

lets take time complexity of a program is O(n)

so, O(f(x)) = f(x)

where f(x) = n

now time complexity of our program:

sum =0;   // here it is a constant so its complexity will be O(1) or it will run at 1 time only

var=0 ; // here also O(1)

product =0 // also here O(1)

for (i =1; i <n+2; i++)

{

      sum= sum+ 2i;       // for this first for loop time complexity will be O(n)

       for( j=i; i< [(n*n) +i] ; j++)

       {

              var =sum+var;    // for this second loop time complexity will be O(n) also. but if we multiply the variable then here time complexity will be in logn but here in program constant part is multiplying

               for(k= sum+var)

                {

                      product = var++; // now for the third loop time complexity will be O(n) also

                  }

         }

}

for(m+1; j<i)

{

   sum= sum + product;   // here for this loop time complexity will be O(n)

}

so, we have these complexity all over and we will add all the complexity now:

= O(1) + O(1) + O(1) + [ O(n) * O(n) * O(n) ] + O(n)

=O(n^3)    // by the big O notation arthematic rule we have to take big value and leave the rest

so for this program time complexity in Big O notation will be O(n^3).


Related Solutions

PYTHON Write the time complexity of the code snippets in Big O notation. Also, add one/two...
PYTHON Write the time complexity of the code snippets in Big O notation. Also, add one/two lines to explain your answer. 1a int a = 0, b = 0; for (int i = 0; i< N; i++) {     a = a + i; } for (int j = 0; j < N; j++) {     b = b + j; } 1b bool isPrime(int N) { if (N == 1) return false; for (x = 2; x <= sqrt(N);...
The following functions have been calculated as the runtime complexity of various algorithms. Identify the Big-O...
The following functions have been calculated as the runtime complexity of various algorithms. Identify the Big-O complexity, and provide witness values to support your answer. Clearly highlight your answer and show any working out you do. i. f(n) = 13 + 3n2 – 9n ii. f(n) = 3n.log2n + 7n iii. f(n) = nn + 2n5 – 7 iv. f(n) = 2log2n + 4n v. f(n) = 20 + 2n4 – n2 +n vi. f(n) = 7n3/4 +3n
Calculate the Big-O time complexity. Show work 1. n^2 + 3n + 2 2. (n^2 +...
Calculate the Big-O time complexity. Show work 1. n^2 + 3n + 2 2. (n^2 + n)(n ^2 + π/2 ) 3. 1 + 2 + 3 + · · · + n − 1 + n
Big-O: Describe the worst case running time of the following pseudocode or functions in Big-Oh notation...
Big-O: Describe the worst case running time of the following pseudocode or functions in Big-Oh notation in terms of the variable n. Show your work a) O( ) int m1(int n, int m) { if (n < 10) return n; else if (n < 100) return happy (n - 2, m); else return happy (n/2, m); } ----------------------------- b) O ( ) void m2 (int n) { j = 0; while (j < n) { for (int i = 0;...
) Write a recurrence relation of the following code and find the time complexity. void towerOfHanoi(int...
) Write a recurrence relation of the following code and find the time complexity. void towerOfHanoi(int n, char from_rod,                     char to_rod, char aux_rod) {     if (n == 1)     {         cout << "Move disk 1 from " << from_rod << " to " << to_rod<<endl;         return;     }     towerOfHanoi(n - 1, from_rod, aux_rod, to_rod);     cout << "Move disk " << n << " from " << from_rod << " to " << to_rod << endl;     towerOfHanoi(n - 1, aux_rod, to_rod, from_rod); }
Give the asymptotic (“big-Oh”) running time complexity of the following algorithm, show all the work you...
Give the asymptotic (“big-Oh”) running time complexity of the following algorithm, show all the work you have done. Algorithm: ArrayMangle(A[ ], int n) Input: an array A, an integer n x = 0; for (i=0; i<=n-1; i++) { for (j=i; j<=n-1; j++) { x = x + A[j]; } for (k=0; k<= n-1; k++) { for (j=0; j< =n-1; j++) { x = x + A[j]*A[k]; } } }
Q1. A. What is the complexity of partition process in quick sort? O(1) O(logN) O(N) O(NlogN)...
Q1. A. What is the complexity of partition process in quick sort? O(1) O(logN) O(N) O(NlogN) B. Evaluate the following postfix expression. 2 3 4 + * C. In an array representation of heap, what are the parent node of a node a[10]? a[9] a[11] a[5] a[20] There is no easy way to access parent node in such representation. D. In an array representation of heap, what are the children nodes (if any) of a node a[10]? a[11] and a[12]...
// 5. Predict what the following code will print (write it down), then try it. var...
// 5. Predict what the following code will print (write it down), then try it. var myVariable = "global"; var myOtherVariable = "global"; function myFunction() { var myVariable = "local"; return myVariable; } function myOtherFunction() { myOtherVariable = "local"; return myOtherVariable; javascript
QUESTION 1 Consider the following program: var x = 0; function sub1() {    var x =...
QUESTION 1 Consider the following program: var x = 0; function sub1() {    var x = 1;    function sub2() {       function sub3() {          print x;       }       sub3();    }    function sub4() {       var x = 2;       sub2();      }    sub4(); } sub1(); If this code uses static scoping, when it is executed what is printed? 0 1 2 QUESTION 2 Consider the following program: var x = 0; function sub1() {    var x = 1;    function sub2() {       function sub3() {          print...
1. What is the Big-O run-time of linear search and binary search in the best, average...
1. What is the Big-O run-time of linear search and binary search in the best, average and worst cases?       Linear Search                  Binary Search Best: Worst: Average: 2. What is the Big-O runtime of the following linked list methods? addFirst() toString() What is the Big-O runtime of the below Stack method? isSorted(Node n) 3. What is the Big-O runtime of the below methods: Method A: Stack Copy Constructor Option 1 public Stack(Stack<T> original) { if(original == null) { return; }...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT