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

Ds 4

The document outlines various programming problems related to time complexity analysis, including functions that sum and multiply array elements, recursive algorithms, and prime number testing. It presents code snippets and asks for their time complexities, with specific examples and explanations. The document also includes questions about the equivalence of different complexity classes.

Uploaded by

Armaan Mohammed
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)
9 views4 pages

Ds 4

The document outlines various programming problems related to time complexity analysis, including functions that sum and multiply array elements, recursive algorithms, and prime number testing. It presents code snippets and asks for their time complexities, with specific examples and explanations. The document also includes questions about the equivalence of different complexity classes.

Uploaded by

Armaan Mohammed
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. Fine the time complexity of the func1 function in the program show in program1.c as follows:

#include <stdio.h>

void func1(int array[], int length) T = K + k2n + kzw


{
int sum = 0;
=
v(n)
int product = 1;
JK1

o(n)
for (int i = 0; i < length; i++)
{
sum += array[i];
-
kz
}

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


{
product *= array[i]; -
13
}
}

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

2. Fine the time complexity of the func function in the program from program2.c as follows:

void func(int n)
{ T= k, + (kz n)n
int sum = 0; ?
int product = 1;
Jk / =
k1 + k2 .

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


kz n2
.

{
for (int j = 0; j < n; j++)
{ o(n2)
printf("%d , %d\n", i, j); -
R2
}
}

}
3. Consider the recursive algorithm above, where the random(int n) spends one unit of time to return a
random integer which is evenly distributed within the range [0,n][0,n]. If the average processing time
is T(n), what is the value of T(6)?

int function(int n)
{ This func. runs 6 times
int i;
=> 6
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)? Why?


a) O(N + P), where P < N/9 -
o(NS
b) 0(9N-k) -

o(N)
c) O(N + 8log N) - o(N)
d) O(N + M ) 2 -
O(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) &


G
{
if (node == NULL)
{
in 2n 4n + + +...
return 0;
} 2n 2n 2n n(k)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){


Runs En-1 times
if (n == 1){
return 0;
o(in-1) =
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++) { o()


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

return 1;
}
isPrime();

You might also like