(1):
Time complexity of IterativeFunctioin is O(n/2) which is equivalent to O(n) because every time we decrease the value of "a" by 2, so it takes (n/2) time to complete the code.
Space complexity of IterativeFunctioin is O(1) because we have used constant space like only variable a and b .
(2):
In the given code,
for i=0, j loop iterates from 0 to n-1, which is for n times and k loop iterates for n times. So for i=0, it takes n*n iterations.
for i=1, j loop iterates from 1 to n-1, which is for n-1 times and k loop iterates for n times. So for i=1, it takes n*(n-1)iterations.
.
.
for i=n-1, j loop iterates from n-1 to n-1, which is for 1 time and k loop iterates for n times. So for i=0, it takes niterations.
So the answer is n + n*2 + n*3 + .... + n*n = n[1+2+...+n] = n[n(n+1)/2] = O(n3)
So T(n) = O(n3)
Time complexity of given code is (n3)
(3):
Algorithm is as follows:
So the time complexity of this efficient algorith is O(n)+O(1) = O(n)
Time complexity : O(n)
Mention in comments if any mistakes or errors are found. Thank you.
Question No.1 [CLO 1][7 marks] 1. Consider the following pseudocode: Algorithm IterativeFunction (a, b) // a...
Consider the following pseudocode: Algorithm RecursiveFunction (a, b) // a and b are integers if (as1 ) return b; else return RecursiveFunction (a-2, a/2); endif a. What is the time complexity of the RecursiveFunction pseudocode shown above? b What is the space complexity of the RecursiveFunction pseudocode shown above? n(n+1) C. What is the time complexity of the following algorithm (Note that 21-, i = n(n+1)(2n+1). and Σ.,1 ): Provide both T(n) and order, Ofn)). int A=0; for (int i=0;i<n;i++)...
Please answer this in python pseudocode. It's an algorithm question. 1. [10 marks] Consider the function SumKSmallest(A[0..n – 1), k) that returns the sum of the k smallest elements in an unsorted integer array A of size n. For example, given the array A=[6,-6,3,2,1,2,0,4,3,5] and k=3, the function should return -5. a. [3 marks) Write an algorithm in pseudocode for SumKSmallest using the brute force paradigm. Indicate and justify (within a few sentences) the time complexity of your algorithm. b....
[10 marks] consider the following pseudocode: p := 0 x := 2fori:= 2ton p := (p + i ) * x a) How many addition(s) and multiplication(s) are performed by the above pseudocode?b) Express the time-complexity of the pseudocode using the big-Θ notation.) Trace the algorithm, below, then answer (c) and (d): Procedure xyz(n: integer) s := 0, for i:= 1 to n, for j:= 1to i, s:= s+ j*(i− j+ 1) return s c) What is the time-complexity for...
When asked to describe an algorithm you are expected to give a clear pseudo-code description of the algorithm 1. (10 pts) Here is a new sorting algorithm NewSort Suppose the original call made is NewSort(A,0,n-1) where A is an array integers. == void NewSort(int A[], int i, int j){ \\ sorts the subarray Aſi..j] if (j i+1) \\when there are only 2 elements if (A[i] > A[j]) swap(A,i,j) \\swaps A[i] and A[j] else { int k = (j-i+1)/3; NewSort(A,i,j-k); \\...
Question 2 Consider the following algorithm Fun that takes array A and key Kas Fun(AO,...,n - 1], K) count = 0 for i = 0 ton - 1 do for j = i +1 to n - 1 do if A[i] + A[j] == K then count = count +1 end if end for end for return count What is the best case time complexity of the above algorithm?! (log(n)) O(1) (n) (na) Previous o H H 9
A) Write the pseudocode for an algorithm using dynamic programming to solve the activity-selection problem based on this recurrence: c[i, j] = 0 if Si; = Ø max {c[i, k] + c[k,j] + 1} if Sij +0 ak eSij B) Analyze the running time (the time complexity) of your algorithm and compare it to the iterative greedy algorithm.
URGENT Question 3 25 pts ArrayMystery: Input: n: a positive integer Pseudocode: Let output be an empty array For i = 1 to n j = 1 While ij <= n Addj to the end of output j - j + 1 Return output Answer the following questions about the ArrayMystery algorithm above. a) How many times will the inner while loop iterate? You should express your answer in terms of i and n, using Big-Oh notation. Briefly justify your...
a. Use pseudocode to specify a brute-force algorithm that takes as input a list of n positive integers and determines whether there are two distinct elements of the list that have as their sum a third element of the list. That is, whether there exists i, j.k such that iヂj, i关k,j关k and ai + aj = ak. The algorithm should loop through all triples of elements of the list checking whether the sum of the first two is the third...
In C++: 1. What is the difference between a deterministic algorithm and a randomized algorithm? 2. What is the difference between a Las Vegas algorithm (pattern) and a Monte Carlo algorithm? 3. Solve the following recurrences, giving the answer in terms of Big-Oh O() F (n) = 4F ( 1 ) +no F (n) = 2 F (*) +2 4. Given the string "DECEMBER", execute the mergesort algorithm by hand to sort it. Show your work to make it clear...
Explain please II. (7 points) Consider the following bit of pseudocode: for (int k = 1; ks Ign; k++) (for (int i = 1; i r: i++) Print "Hello World"; for (int j-1;jsn:j++) Print "Hello World" when n = 2, how many times will "Hello World" be printed? When n 4, how many times will "Hello World be printed? O Assuming that the print is the basic operation, what is the complexity function of this pseudocode? II. (7 points) Consider...