Question

In: Computer Science

1. Give the O-runtime depending on n for the code snippet below (n > 0). for(...

1. Give the O-runtime depending on n for the code snippet below (n > 0).

for( j = n; j <= 0; j = j - 1)

                                    for( k = 1; k <= n; k = k + 1)

                                                print(" ");

2. Give the O-runtime depending on n for the code snippet below (n > 0).

           for( j = 1; j <= n; j = j + 1)

                                    for( k = 1; k <= n; k = k + 2)

                                                print(" ");

Solutions

Expert Solution

Answer 1:

O-runtime is O(1) for the code snippet below (n > 0).

for( j = n; j <= 0; j = j - 1) //STATEMENT 1

                                    for( k = 1; k <= n; k = k + 1) // STATEMENT 2

                                                print(" "); // STATEMENT 3

EXPLANATION :

The STATEMENT1 runs for no times. The value of j , it is initialised as j = n and the condition never becomes true because n > 0 ( given in question).

So, The inner statements ie. STATEMENT 2 , STATEMENT 3 then will not execute for any value of n,

Hence, the runtime complexity of above code is O( 1 ).

Answer 2. O-runtime depending is O ( n 2 )   for the code snippet below (n > 0).

           for( j = 1; j <= n; j = j + 1) // STATEMENT 1

                                    for( k = 1; k <= n; k = k + 2) // STATEMENT 2

                                                print(" "); //STATEMENT 3

EXPLANATION :

The STATEMENT1 runs for n times ( from j=1 to j=n) , and STATEMENT2 runs for ( n / 2 ) times. And

STATEMENT3 runs constant time.

So, Correct Complexity is ( n * ( n / 2 ) ) = ( n2 / 2 ) = n2 ( approx).

Hence, the O runtime of above code is O ( n2 ) .


Related Solutions

[50%] Code Snippet Given snippet code below that you are required to complete. You are not...
[50%] Code Snippet Given snippet code below that you are required to complete. You are not allowed to make a new function or change any given code. Please complete each section that are marked with the notation “INSERT YOUR CODE HERE”. Once you complete the snippet below, your output should have the same result with the given output below. Descriptions: [15%] isValid() This function is for checking that there is no duplicated employee data in the linked list. This function...
Given the below Java code snippet, assuming the code is correct, and the names are meaningful,...
Given the below Java code snippet, assuming the code is correct, and the names are meaningful, select the best answer for the below questions: public static void main(String[] args) { int numStations = 10; int enter = 0; int exit = 0; Train train = new Train(numStations); for (int station = 0; station < numStations; station++) {      enter = enter + train.enterPassengers(station);      exit = exit + train.exitPassengers(station); } System.out.println("Number of passengers entered the train: " + enter); System.out.println("Number...
a) Find a bug in the code snippet below. Assume that a, b, c and d...
a) Find a bug in the code snippet below. Assume that a, b, c and d are correctly declared and initialized integer variables. a = b+c if (a=d) then print *,”A equals to D” else print *,”A does not equal D” end if b) Find a bug in the code snippet below. Assume that a,b,and c are correctly declared and initialized integer variables. if (a+b) > c) then print *,”Sum of A+B is greater than C” end if
What is the Big-Oh notation of the following code snippet: BinarySearch(numbers, N, key) { mid =...
What is the Big-Oh notation of the following code snippet: BinarySearch(numbers, N, key) { mid = 0; low = 0; high = 0; high = N - 1; while (high >= low) { mid = (high + low) / 2 if (numbers[mid] < key) { low = mid + 1 } else if (numbers[mid] > key) { high = mid - 1 } else { return mid } } return -1 // not found }
What is the value of n after this code runs? 1 Point int n = 0;...
What is the value of n after this code runs? 1 Point int n = 0; int j = 7; if ((j!=0) && (n < 25)) { n = 1; if (j > 4) { n = 2; } else { n = 3; } } else { n = 4; if (j%2 >= 2) { n = 5; } else { n = 6; } }
Given the code snippet below, complete this program by: Displaying the data (temperatures) Displaying the minimum...
Given the code snippet below, complete this program by: Displaying the data (temperatures) Displaying the minimum temperature value Displaying the maximum temperature value Displaying the mean temperature SUBMIT THE PYTHON FILE (.PY) NOT SCREENSHOT, WORD DOCUMENT ETC. IF YOUR SUBMISSION IS NOT A .PY FILE, AUTOMATIC ZERO. #Code snippet below. Copy this into your favorite IDE and complete the tasks import numpy as np import pandas as pd temps = np.random.randint(60, 101, 6) temperatures = pd.Series(temps) 2. Write a python...
# O W L S f(O,W,L,S) 0 0 0 0 0 0 1 0 0 0...
# O W L S f(O,W,L,S) 0 0 0 0 0 0 1 0 0 0 1 0 2 0 0 1 0 1 3 0 0 1 1 1 4 0 1 0 0 0 5 0 1 0 1 1 6 0 1 1 0 1 7 0 1 1 1 X 8 1 0 0 0 0 9 1 0 0 1 0 10 1 0 1 0 0 11 1 0 1 1 1 12 1...
Viewing the code snippet below answer the following questions: int num=10; do {      cout << “Hello”...
Viewing the code snippet below answer the following questions: int num=10; do {      cout << “Hello” << endl;      num--; } while(num > 1); After the above code is executed: “Hello” printed how many times: ___________ What is value ofnum:  ____________ language is c++
Task 1. to be completed. 1.1 - Write a small Python snippet code that can revert...
Task 1. to be completed. 1.1 - Write a small Python snippet code that can revert an array of N element (Please do not use the default function “reverse()). 1.2 - There is a famous programming (algorithm) concept that can vastly reduce the complexity of Recursive Fibonacci Algorithm. 2 .a) What is the name of this concept 2.b) Write the execution time and memory complexity of Fibonacci sequences, once this concept is applied 2.c) Explain the main idea behind this...
For each n ∈ N, let fn : [0, 1] → [0, 1] be defined by...
For each n ∈ N, let fn : [0, 1] → [0, 1] be defined by fn(x) = 0, x > 1/n and fn(x) = 1−nx if 0 ≤ x ≤1/n. The collection {fn(x) : n ∈ N} converges to a point, call it f(x) for each x ∈ [0, 1]. Show whether {fn(x) : n ∈ N} converges to f uniformly or not.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT