Question

In: Computer Science

What is the Θ( ) for each code segment below? (a) for( int i = 1,...

What is the Θ( ) for each code segment below?

(a) for( int i = 1, i<= n, i = i*2){

      some constant operation

}

(b) for( int i = 1, i<= n, i++){

     for( int j = 2*i, j<=n, j++){

          some constant operation

     }

}

(c) for( int i = 1, i<= n, i = i*2){

     for( int j = 1, j<=n, j = j*2){

          some constant operation

     }

}

Solutions

Expert Solution

This code has one loops

Values of i during first loop are from 1,2,4,8,..,n
So, First loop running for log(n) times

Time complexity
= O(Number of times loop runs)
= O(log(n))

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

This code has two loops

Values of i during first loop are from 1,2,3,..,n
So, First loop running for n times

Values of j during first loop are from 2*i,2*i+1,,..,n
So, Second loop running for n-2i times
So, in worst case second loop running for n times

Time complexity
= O(Product of number of times both loop runs)
= O(n*n)
= O(n^2)

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

This code has two loops

Values of i during first loop are from 1,2,4,8,..,n
So, First loop running for log(n) times

Values of j during first loop are from 1,2,4,8,..,n
So, Second loop running for log(n) times

Time complexity
= O(Product of number of times both loop runs)
= O(log(n)*log(n))
= O(log(n)^2)

Related Solutions

Show the output of the following code segment. int count=0;                         for (int i=2; i <=...
Show the output of the following code segment. int count=0;                         for (int i=2; i <= 4; i++ ) {                                     StdOut.println(i);                                     for (int j=1; j <3; j++) {                                                 count++;                                                 StdOut.println(i +" " + j +" "+ count);                                     }                         } count i j I print 0 2 1 3 1 1 1 2 3 2 2 3 3 3 3 4 3 Show the output of the function call statements shown.             double x =...
What is the ouput of the following code? void loop(int num) { for(int i = 1;...
What is the ouput of the following code? void loop(int num) { for(int i = 1; i < num; ++i) { for(int j = 0; j < 5; ++j) { cout << j; } } } int main() { loop(3); return 0; }
C++ Q2.   Given the code as below. Which statement is correct? int myAry[100]; for(int i=0; i<100;...
C++ Q2.   Given the code as below. Which statement is correct? int myAry[100]; for(int i=0; i<100; i++) myAry = i + 2; for(int i=100; i>0; i--) cout << myAry[i] << '\t'; The first for loop assigns myAry 99 values and the null character. The second for loop prints out myAry elements backwards. The index in the second for loop is out of bounds. Only the first loop needs the null character. Q3. A value returning function that takes one parameter,...
Consider the following code: void swap(int arr[], int i, int j) {        int temp = arr[i];...
Consider the following code: void swap(int arr[], int i, int j) {        int temp = arr[i];        arr[i] = arr[j];        arr[j] = temp; } void function(int arr[], int length) {        for (int i = 0; i<length / 2; i++)               swap(arr, i, (length / 2 + i) % length); } If the input to the function was int arr[] = { 6, 1, 8, 2, 5, 4, 3, 7 }; function(arr,8); What values would be stored in the array after calling the...
1)What is the output of the following code? struct someType { int a; int b; };...
1)What is the output of the following code? struct someType { int a; int b; }; void f1(someType &s) { s.a = 1; s.b = 2; } someType f2(someType s) { someType t; t = s; s.a = 3; return t; } int main() { someType s1, s2; f1(s1); s2 = f2(s1); cout << s1.a << '-' << s1.b << '-' << s2.a << '-' << s2.b << '-' << endl; return 0; } ------------------------------------------------------------------------------------------------------------------ 2) struct dateType { int...
Question 31 Given the code snippet below, what prints? void fun(int *p) { int q =...
Question 31 Given the code snippet below, what prints? void fun(int *p) { int q = 10; p = &q; } int main() { int r = 20; int *p = &r; fun(p); cout << *p; return 0; } Question 31 options: 10 20 compiler error Runtime error Question 32 A union’s members are exactly like the members of a Structure. Question 32 options: True False Question 33 Given the code below, what are the errors. #include <iostream> using namespace...
1. Convert the following code shown below to C++ code: public class HighwayBillboard { public int...
1. Convert the following code shown below to C++ code: public class HighwayBillboard { public int maxRevenue(int[] billboard, int[] revenue, int distance, int milesRes) { int[] MR = new int[distance + 1]; //Next billboard which can be used will start from index 0 in billboard[] int nextBillBoard = 0; //example if milesRes = 5 miles then any 2 bill boards has to be more than //5 miles away so actually we can put at 6th mile so we can add...
For a fixed θ>0, let X1,X2,…,Xn be i.i.d., each with the beta (1,θ) density. i) Find...
For a fixed θ>0, let X1,X2,…,Xn be i.i.d., each with the beta (1,θ) density. i) Find θ^ that is the maximum likelihood estimate of θ. ii) Let X have the beta (1,θ) density. Find the density of −log⁡(1−X). Recognize this as one of the famous ones and provide its name and parameters. iii) Find f that is the density of the MLE θ^ in part (i).
Given int B[4][4]={...}; Write a code segment that exchange the values about the main diagonal of...
Given int B[4][4]={...}; Write a code segment that exchange the values about the main diagonal of the matrix.
C CODE PLZ! All instructions for what to do in code #include <stdio.h> int main(int argc,...
C CODE PLZ! All instructions for what to do in code #include <stdio.h> int main(int argc, char **argv) { int n, k, l, r, t, d, i; char str[65]; /* Make the user enter a non-negative integer */ printf("Please enter a non-negative integer: "); scanf("%d", &n); while (n < 0) { printf("Sorry, your input is incorrect.\n"); printf("Please enter a non-negative integer: "); scanf("%d", &n); } /* Convert the integer to reversed binary: e.g. 6 gets converted to 011 */ if...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT