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

Time Complexity Practice Exercises

The document discusses time complexity questions related to analyzing the efficiency of algorithms. It includes questions about determining the time complexity of functions with nested loops and recursive calls. It also asks about determining if specific time complexities are equivalent to O(N) and finding the time complexity of algorithms that check if a number is prime or sums nodes in a binary search tree.
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)
107 views4 pages

Time Complexity Practice Exercises

The document discusses time complexity questions related to analyzing the efficiency of algorithms. It includes questions about determining the time complexity of functions with nested loops and recursive calls. It also asks about determining if specific time complexities are equivalent to O(N) and finding the time complexity of algorithms that check if a number is prime or sums nodes in a binary search tree.
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
  • Techniques to Calculate Time Complexity
  • Practice Exercises

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)


{
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};
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)
{
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 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)
{
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)? Why?


a) O(N + P), where P < N/9
b) 0(9N-k)
c) O(N + 8log 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([Link]) + [Link] + sum([Link]);
}
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;
}
isPrime();

Common questions

Powered by AI

The time complexity of the function 'func' is O(n^2). This is due to the presence of nested loops, where each loop runs 'n' times. The outer loop runs 'n' times, and for each iteration of the outer loop, the inner loop also runs 'n' times, leading to n * n = n^2 total iterations, which determines the quadratic time complexity .

The complexities equivalent to O(N) are a) O(N + P), where P < N/9, and c) O(N + 8log N). Complexity a) remains O(N) because P grows slower than N and does not affect the leading term. In complexity c), the logarithmic component (8log N) grows much slower than the linear component, so it is also simplified to O(N). Complexities b) O(9N-k) and d) O(N + M^2) are not equivalent; the former due to constant factors and the latter because M^2 could grow larger than N .

The second 'isPrime' function has a time complexity of O(1), assuming the loop runs with a constant upper limit of 10000. Unlike traditional algorithms which check divisors up to √n, this loop is hard-coded to iterate up to √10000, a fixed value, independently of 'n'. Therefore, it does not scale with the input but rather checks divisibility within a predetermined range, unsuitable for large 'n' .

The average processing time T(6) can be determined by analyzing the recursive function. The recursion divides the problem into smaller subproblems split randomly, leading the function to call itself twice with different parameters based on 'i', a random integer less than 'n'. Solving this precisely requires probabilistic analysis, often done using recurrence relations. For T(n), the recurrence can be approximated by T(n) ≈ n log n, typical for recursive tree structures. However, calculating for T(6) specifically without more specific recursive relation analysis is complex, especially without defining random(n).

The time complexity of the function 'func1' is O(n). This is because it consists of two separate loops that each iterate over the entire array, resulting in a linear time complexity. The first loop calculates the sum of array elements, and the second loop calculates the product, both operating independently with O(n) complexity. Hence, the overall time complexity remains O(n).

The complexity of the given 'isPrime' function for checking if a number is prime is O(√n). This is because the loop iterates from 2 up to the square root of 'n'. For any non-prime number 'n', at least one factor will be less than or equal to √n, allowing detection of non-primality without inspecting the entire range, thereby improving efficiency to O(√n).

The runtime for summing values in a balanced binary search tree using this recursive function is O(n), where 'n' is the number of nodes in the tree. The function performs a recursive traversal that visits each node exactly once (depth-first search), hence processing every node leads to a linear time complexity .

 
 
 
URBAN
Time Complexity – Competitive Practice Sheet 
 
1. Fine the time complexity of the func1 function in the program show in pr
3. Consider the recursive algorithm above, where the random(int n) spends one unit of time to return a 
random integer which
return 1; 
} 
 
7. What is the time complexity of the following snippet of code? 
int isPrime(int n){  
 
    for (int i

You might also like