Question

In: Computer Science

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.

Solutions

Expert Solution

Solution:

===============

Here for ( int i = 0; i < n; i++) will be executed exactly n time from i=0 to i=n-1

for each value of i inner for ( int j = i; j > 0; j--) will be executed i times.

thus s=s+1 will be executed in the following way

when i=0 inner for loop will execute 0 times

when i=1 inner for loop will execute 1 times

when i=2 inner for loop will execute 2 times

when i=3 inner for loop will execute 3 times

-----------------

--------------

-------------

when i=n-1 inner for loop will execute n-1 times

Hence total number of times  s = s + 1; will be executed is 0+1+2+3+4+5+6...........(n-1)

This sum is in AP thus its sum is gievn by   = n/2(2a+ (n-1)d)

here a is 0

d is 1

n is n-1

Hence sum is

Ans

Part b) TIme complexity of this function depends on how many times  s = s + 1; statement is executed from above result number of times statement is executed is (n)(n-1)/2 = (n^2-n)/2 Here leading term is n^2 for very large value of n effect of (n)/2 can be neglacted thus time Complexity is given by


Related Solutions

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...
int f2 (int n) j = 0; while (j <n) {for (int i = 0; i...
int f2 (int n) j = 0; while (j <n) {for (int i = 0; i <n; ++ i) {cout << "j =" + j; j = j + 5; }}
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;...
Consider the following recursive method in Java public static int mystery(int n) {   if (n ==...
Consider the following recursive method in Java public static int mystery(int n) {   if (n == 0)   return 1;    else    return 4 * mystery (n - 1);   } What is the output of  mystery (3) using the code segment above Show your work on your trace file
Consider the following algorithm. Algorithm Mystery(n) //Input: A nonnegative integer n S ← 0 for i...
Consider the following algorithm. Algorithm Mystery(n) //Input: A nonnegative integer n S ← 0 for i ← 1 to n do S ← S + i * i return S a. What does this algorithm compute? b. What is its basic operation? c. How many times is the basic operation executed? d. What is the efficiency class of this algorithm? e. Suggest an improvement, or a better algorithm altogether, and indicate its efficiency class. If you cannot do it, try...
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.
Consider a Markov chain {Xn|n ≥ 0} with state space S = {0, 1, · ·...
Consider a Markov chain {Xn|n ≥ 0} with state space S = {0, 1, · · · } and transition matrix (pij ) given by pij = 1 2 if j = i − 1 1 2 if j = i + 1, i ≥ 1, and p00 = p01 = 1 2 . Find P{X0 ≤ X1 ≤ · · · ≤ Xn|X0 = i}, i ≥ 0 . Q2. Consider the Markov chain given in Q1. Find P{X1,...
Write a Haskell function combine :: Int -> Int -> Int -> Int with the following...
Write a Haskell function combine :: Int -> Int -> Int -> Int with the following behavior: • When x, y, and z all correspond to digit values (i.e., integers between 0 and 9, inclusive), combine x y z returns the integer given by the sequence of digits x y z. (That is, x is treated as the digit in the hundreds place, y is treated as the digit in the tens place, and z is treated as the digit...
Consider the following program specification: Input: An integer n > 0 and an array A[0..(n –...
Consider the following program specification: Input: An integer n > 0 and an array A[0..(n – 1)] of n integers. Output: The largest index b such that A[b] is the largest value in A[0..(n – 1)]. For example, if n = 10 and A = [ 4, 8, 1, 3, 8, 5, 4, 7, 1, 2 ] (so A[0] = 4, A[1] = 8, etc.), then the program would return 4, since the largest value in A is 8 and...
Consider the given function. Evaluate the Riemann sum for 0 ≤ x ≤ 3, with n...
Consider the given function. Evaluate the Riemann sum for 0 ≤ x ≤ 3, with n = 6, taking the sample points to be right endpoints.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT