Question

In: Computer Science

Find the runtime of this function, where n is an integer. int function(int n) { int...

Find the runtime of this function, where n is an integer.

int function(int n) {
  int a = 0;
  for (int i = n; i > 0; i--) {
    for (int j = 0; j < i; j++) {
      a = a + i + j;
    }
  }
  return a;
}

Find the runtime of this function, where m and n are two integers.

int f(int m, int n) {
  if (m==1 || n==1) {
    return 1;
  }
  return f(m, n-1) + f(m-1, n);
}

Please explain to me each of the steps and how you receive the answer.

Thank you

Solutions

Expert Solution

Please find the answer for the above given question.

ANSWER :

For Question 1 :

int function(int n) {
  int a = 0;
  for (int i = n; i > 0; i--) {
    for (int j = 0; j < i; j++) {
      a = a + i + j;
    }
  }
  return a;
}

answer : O(n^2) [Big O of n square]

Explanation :

For an example if we consider the n value as 5 . Outer loop will run for 5 times total , we can say that directly based on the condition and decrement that is happening in the first loop. But the inside loop will run based on the i value that is coming from the outer loop. So we write step by step , we can observe the below thing.

when i value is 5 , j loop also will run for 5 times,

when i value is 4 , j loop also will run for 4 times

when i value is 3 , j loop also will run for 3 times

when i value is 2 , j loop also will run for 2 times

when i value is 1 , j loop also will run for 1 times

So if we calculate the number of runs total 5+4+3+2+1 = n(n+1)/2 = O(n^2)/2 + O(n/2) = O(n^2) + O(n) = O(n^2)

Time complexity = O(n^2)

For Question 2 :

int f(int m, int n) {
  if (m==1 || n==1) {
    return 1;
  }
  return f(m, n-1) + f(m-1, n);
}

answer :   O(m+n) [BIg O of m plus n]

Explanation :

For an example if we consider m value as 6 and n value as 7 , So function will be called initially with m and n values as 6 and 7 respectively. until unless m value or n value is 1 recursive calls will happen. So the first recursive call will happen with m value as same but the n value will be decremented by 1 and other function call is m value is decremented by 1 and n value is same.

So the first function will run for n times and the second function will run for m times.

Number of times call will happen to function f is = m+n

Time complexity = O(m+n)

Thanks..


Related Solutions

In C++, type a function function(int n, int base) that converts a positive integer x to...
In C++, type a function function(int n, int base) that converts a positive integer x to any base between 2 and 9. The function HAS to do this using a stack, and using methods from below: +isEmpty(): boolean +push(newEntry: ItemType): boolean +pop(): boolean +peek(): ItemType (Also, type a program to test the function). Hint: could use simple iteration continually divides the decimal number by base and keeps track of the remainder by using a stack.
Write a C function boolean isPrime (int n), that would take a positive integer n as...
Write a C function boolean isPrime (int n), that would take a positive integer n as a parameter and return true or false whether the number is a prime number. You should check the range of n and print proper message (For example, if n is a genitive number, you should print out an error message).
Suppose the runtime of a computer program is proportional to 2^n where n is the input...
Suppose the runtime of a computer program is proportional to 2^n where n is the input size. When given an input of size 1024, suppose the runtime is 256ms. For what input size would the program have runtime of 1ms? A program takes time proportional to the input size. If the program takes 36 milliseconds for input size 30, how  many milliseconds does it take for input size 10? A program takes time T(n) = k2n  where k is some constant and...
The runtime of a program is proportional to n1.5 where n is the input size.  For input...
The runtime of a program is proportional to n1.5 where n is the input size.  For input size 100 the runtime is 51 ms.  What is the new runtime when the input size is quadrupled?
Given a monotonically increasing function f(n) (where ‘n’ is an integer), use a binary search algorithm...
Given a monotonically increasing function f(n) (where ‘n’ is an integer), use a binary search algorithm to find the largest value of ‘n’ for which f(n) is less than a target. Show all the work (including the values for the left index, right index, middle index and the function value at the middle index) for each iteration as well as write down the initial values of the left index and right index and the corresponding function values. f(n) = 3n^3...
Consider the following function: int counter( int n) { int s = 0;    for ( int...
Consider the following function: int counter( int n) { int s = 0;    for ( int i = 0; i < n; i++)             for ( int j = i; j > 0; j--)                        s = s + 1;   return s; } Part (a): How many times "s=s+1;" will be executed? Determine a precise count.  Show all your work. Part (b): What is the time complexity of the "counter" function in terms of Big-O notation? Justify and show all your work.
#include <stdio.h> int sum(int n); //prototype int main() {     int number, result;     printf("Enter a positive integer:...
#include <stdio.h> int sum(int n); //prototype int main() {     int number, result;     printf("Enter a positive integer: ");     scanf("%d", &number);     result = sum(number);     printf("sum = %d", result);     return 0; } int sum(int n) {     if (n != 0)         return n + sum(n-1);     else         return n; } What does the code above do?
Consider the following C code that outlines Fibonacci function int fib (int n) { if (n...
Consider the following C code that outlines Fibonacci function int fib (int n) { if (n == 0) return 0; else if (n==1) return 1; else return fib(n-1) + fib (n-2); } For this programming assignment, write and test an ARMv8 program to find Fibonacci (n). You need to write a main function that calls the recursive fib function and passes an argument n. The function fib calls itself (recursively) twice to compute fib(n-1) and fib (n-2). The input to...
Write a C function, for example void sumOfPrime(int n), that takes a positive int n as...
Write a C function, for example void sumOfPrime(int n), that takes a positive int n as a parameter, put all prime numbers less than n into an array, and print out the array and the sum of the numbers in that array. You can use the isPrime function above to save time.
What is time Complexity each of the following function? 1- void function(int n) {             for (int...
What is time Complexity each of the following function? 1- void function(int n) {             for (int i=n/2; i<=n; i++)                           for (int j=1; j<=n/2; j++)                                     for (int k=1; k<=n; k = k * 2)                                                 print ”Hello”; } 2- void function(int n) {             for (int i=n/2; i<=n; i++)                           for (int j=1; j<=n; j = 2 * j)                                     for (int k=1; k<=n; k = k * 2)                                                 print ”Hello”; } 3- void function(int n) {             for (int i=1; i<=n; i++)                           for (int j=1;...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT