Question

In: Computer Science

What is the complexity of the given code as a function of the problem size n?  Show...

What is the complexity of the given code as a function of the problem size n?  Show all of the details of your analysis.

for (int i = 0; i < 2*n; i++)

{

   if (i == n)

   {

      for (int j = 0; j < i; j++)

         for (int k = 0; k < i; k++)

            O(1)

   }

   else

   {

      for (int j = 0; j < i; j++)

         O(1)

   }

}

Solutions

Expert Solution

for (int i = 0; i < 2*n; i++)
{
if (i == n)
{
for (int j = 0; j < i; j++)
  for (int k = 0; k < i; k++)
  O(1)
}
else
{
   for (int j = 0; j < i; j++)
  O(1)
   }
}

Analysis :
When i < n :

else condition will work which has a loop that goes i times and a constant operation inside that.
Thus , for i = 0-n-1,
Number of opeartions = 0,1,2,3,.....n-1
Total opeartion = n*(n-1)/2 equivalent to O(n^2)

when i == n :
This condition will come only 1 time
There are two for loops that goes n times each because i==n
Thus , total operation = n*n equivalent to O(n^2)

when i > n :
else condition will work which has a loop that goes i times and a constant operation inside that.
Thus , for i = n+1 to 2*n-1,
Number of opeartions = (n+1),(n+2)......(2*n-1)
Total opeartion = (n-1)(n+1 + 2*n -1)/2 [ ap sum = n/2(first term + last term) ]
=(n-1)(3n)/2 equivalent to O(n^2)

Thus ,
The overall complexity = O(n^2 + n^2 + n^2) = O(3n^2) which is equivalent to O(n^2)


Related Solutions

//2.-------------------------------------------------------------- Given an array of size N, what is the complexity? int m = A[0]; for...
//2.-------------------------------------------------------------- Given an array of size N, what is the complexity? int m = A[0]; for (int i=1; i<N; i++) { if (A[i] > m) m = A[i]; } int k = 0; for (int i=0; i<N; i++) { if (A[i] == m) k++; } What is a (name one) most frequent operation performed?______________________ Expression showing the number of times it is performed ___________ Time Complexity in Big-O Notation O( ) _____________________________________________________ //3.-------------------------------------------------------------- int mult(int N, int M) {   ...
Given an unsorted integer array A of size n, develop an pseudocode with time complexity as...
Given an unsorted integer array A of size n, develop an pseudocode with time complexity as low as possible to find two indexes i and j such that A[i] + A[j] = 100. Indexes i and j may or may not be the same value.
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;...
Q)Determine the complexity of the following code chunks. Show all calculations. (Assume that f() has complexity...
Q)Determine the complexity of the following code chunks. Show all calculations. (Assume that f() has complexity O(1)). A)for(i=0; i<n; i++) m += i; B) for(i=0; i<n; i++)                   for(j=0; j<n; j++)                             sum[i] += entry[i][j]; C) for(i=0; i<n; i++)                   for(j=0; j<i; j++)                             m += j; D) i= 1; while(i< n) { tot += i; i= i* 2;} E) i= n; while(i> 0) { tot += i; i= i/ 2; } F) for(i=0; i<n; i++)                  for(j=0; j<n; j++)...
assume n = 2^100. consider the following problem. given an unsorted list of size n you...
assume n = 2^100. consider the following problem. given an unsorted list of size n you can choose to sort it or not first. after this step, you get m queries q1, q2, ... qm. one by one of the form "is qi in the list?" Describe whether you would sort and what algorithm you would use if 1.) m= 10, and 2) m = 1000
Finding the complexity of Finding the largest two elements in a queue of size n+3 using...
Finding the complexity of Finding the largest two elements in a queue of size n+3 using Naïve search. with explain
What is the code in Rstudio or R? (a) Generate 200 random samples of size n...
What is the code in Rstudio or R? (a) Generate 200 random samples of size n = 10 from a Poisson distribution with mean λ = 12. i. Calculate sample means for each sample. Report the first 10 sample means. ii. Draw a histogram of the sample means (where the y-axis is the density) and fit a density estimate (default density estimator is ok). iii. What is your finding about the sampling distribution of the sample mean, based on your...
mips code for n queen problem
mips code for n queen problem
Implement the following functions in the given code: def babylonian_square_root(N, estimate, precision): This function is provided...
Implement the following functions in the given code: def babylonian_square_root(N, estimate, precision): This function is provided for you to use: def close_enough(x, y, maximum_allowable_difference): My biggest piece of advice to you is to go one line at a time and check your return values after each little change you make. Starter code: # There are different ways of implementing the same idea in math. # Today we will explore a simple way to calculate square roots. # # # #...
Calculate the Big-O time complexity. Show work 1. n^2 + 3n + 2 2. (n^2 +...
Calculate the Big-O time complexity. Show work 1. n^2 + 3n + 2 2. (n^2 + n)(n ^2 + π/2 ) 3. 1 + 2 + 3 + · · · + n − 1 + n
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT