Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
8 views4 pages

05 How To Calculate Time Complexity of An Algorithm

The document contains a competitive practice sheet focused on time complexity analysis of various functions and algorithms. It includes code snippets in C, asking for time complexity evaluations, recursive function analysis, and equivalence to O(N). Additionally, it addresses the runtime of a binary search tree summation and prime number testing algorithms.

Uploaded by

Iron Man
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views4 pages

05 How To Calculate Time Complexity of An Algorithm

The document contains a competitive practice sheet focused on time complexity analysis of various functions and algorithms. It includes code snippets in C, asking for time complexity evaluations, recursive function analysis, and equivalence to O(N). Additionally, it addresses the runtime of a binary search tree summation and prime number testing algorithms.

Uploaded by

Iron Man
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

URBAN

Time Complexity – Competitive Practice Sheet

1. Find the time complexity of the function

#include <stdio.h>

void function (int array[], int length) {


int sum = 0;
int product = 1;

for (int i = 0; i < length; i++) {


sum += array[i];
}

for (int i = 0; i < length; i++) {


product *= array[i];
}
}

int main() {
int arr[] = {3, 5, 66};
function(arr, 3);
return 0;
}

2. Find the time complexity of the function

void function (int n) {


int sum = 0;
int product = 1;

for (int i = 0; i < n; i++) {


for (int j = 0; j < n; j++) {
printf("%d , %d\n", i, j);
}
}
}
3. Consider the recursive algorithm below, where the random (int n) spends
one unit of time to return a random integer which is evenly distributed
within the range [0,n]. If the average processing time is T(n), what isthe
value of T(6) ?

int function (int n) {


int i;
if (n <= 0) {
return 0;
}
else {
i = random(n - 1);
printf("this\n");
return function(i) + function(n - 1 - i);
}
}

4. Which of the following are equivalent to O(N)

a) O (N + P), where P < N/9


b) 0 (9N - k), where k is a constant
c) O (N + 8 log N)
d) O (N + M2)

5. The following simple code sums the values of all the nodes in a balanced
binary search tree. What is its runtime?

int sum (Node node) {


if (node == NULL) {
return 0;
}
return sum(node.left) + node.value + sum(node.right);
}
6. Find the complexity of the following code which tests whether a give
number is prime or not?

int isPrime (int n) {


if (n == 1) {
return 0;
}

for (int i = 2; i * i < n; i++) {


if (n % i == 0)
return 0;
}
return 1;
}

7. What is the time complexity of the following snippet of code?

int isPrime (int n) {


for (int i = 2; i * i < 10000; i++) {
if (n % i == 0) {
return 0;
}
}
return 1;
}

You might also like