Question

Given the following algorithm:

Algorithnm Input: a1, a2,...,an, a sequence of numbers n, the length of the sequence x, a number Output: ?? i:- 1 While (x2 #

0 0
Add a comment Improve this question Transcribed image text
Answer #1

a) The output is -1. Here x = 4, so x2 = 16. Now the algorithm compares x2 with all the elements of the input sequence (3, -1, 2, 9, 36, -7). Since none of them is equal to 16, the while loop keeps running until i = 6. At this point the condition i < n is no longer satisfies and thus the while loop break and since a6 = -7 which is not equal to 16, so -1 is printed.

b) Worst case happens when no element in the input sequence is equal to x2. If this is the case, the while loop keeps running until i = n, at which point the condition i < n is no longer satisfied and thus the loop breaks, All other operations except the loop are constant time i.e. O(1). Now, since the variable i increments by one in every iteration of the loop, so there are n possible iterations of the loop in worst case. Thus , the time complexity in worst case is O(n).

Add a comment
Know the answer?
Add Answer to:
Given the following algorithm: Algorithnm Input: a1, a2,...,an, a sequence of numbers n, the length of the sequence x, a number Output: ?? i:- 1 While (x2 # a, and i < n) i+1 End-while If (x- - a)...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Exercise 7.3.5: Worst-case time complexity - mystery algorithm. The algorithm below makes some changes to an...

    Exercise 7.3.5: Worst-case time complexity - mystery algorithm. The algorithm below makes some changes to an input sequence of numbers. MysteryAlgorithm Input: a1, a2....,an n, the length of the sequence. p, a number Output: ?? i != 1 j:=n While (i < j) While (i <j and a < p) i:= i + 1 End-while While (i <j and a 2 p) j:=j-1 End-while If (i < j), swap a, and a End-while Return( aj, a2,...,an) (a) Describe in English...

  • Describe in pseudocode an algorithm that takes as input a sequence of distinct numbers a0, a1, .....

    Describe in pseudocode an algorithm that takes as input a sequence of distinct numbers a0, a1, ..., an and returns 1 if the sequence is ordered increasingly, -1 if the sequence is in decreasing order, and 0 otherwise. What are the worse-case, best-case, and avergae-case of the algorithm? Analyze the running time in each of the three cases using asymptotic notation.

  • The input consists of n numbers a1, a2, . . . , an and a target...

    The input consists of n numbers a1, a2, . . . , an and a target value t. The goal is to determine in how many possible ways can we add up two of these numbers to get t. Formally, your program needs to find the number of pairs of indices i, j, i < j such that ai+aj = t. For example, for 2, 7, 3, 1, 5, 6 and t = 7, we can get t in two...

  • What is the worst-case asymptotic time complexity of the following divide-andconquer algorithm (give a Θ-bound). The...

    What is the worst-case asymptotic time complexity of the following divide-andconquer algorithm (give a Θ-bound). The input is an array A of size n. You may assume that n is a power of 2. (NOTE: It doesn’t matter what the algorithm does, just analyze its complexity). Assume that the non-recursive function call, bar(A1,A2,A3,n) has cost 3n. Show your work! Next to each statement show its cost when the algorithm is executed on an imput of size n abd give the...

  • What is the worst-case asymptotic time complexity of the following divide-andconquer algorithm (give a Θ-bound). The...

    What is the worst-case asymptotic time complexity of the following divide-andconquer algorithm (give a Θ-bound). The input is an array A of size n. You may assume that n is a power of 2. (NOTE: It doesn’t matter what the algorithm does, just analyze its complexity). Assume that the non-recursive function call, bar(A1,A2,A3,n) has cost 3n. Show your work! Next to each statement show its cost when the algorithm is executed on an imput of size n abd give the...

  • (V). Given the following algorithm, answer relevant questions. Algorithm 1 An algorithm 1: procedure WHATISTHIS(21,22,...,n: a...

    (V). Given the following algorithm, answer relevant questions. Algorithm 1 An algorithm 1: procedure WHATISTHIS(21,22,...,n: a list of n integers) for i = 2 to n do c= j=i-1 while (j > 0) do if ra; then break end if 4j+1 = a; j= j-1 end while j+1 = 1 end for 14: return 0.02. 1, 15: end procedure Answer the following questions: (1) Run the algorithm with input (41, 02, 03, 04) = (3, 0, 1,6). Record the values...

  • You are given a set of integer numbers A = {a1, a2, ..., an}, where 1...

    You are given a set of integer numbers A = {a1, a2, ..., an}, where 1 ≤ ai ≤ m for all 1 ≤ i ≤ n and for a given positive integer m. Give an algorithm which determines whether you can represent a given positive integer k ≤ nm as a sum of some numbers from A, if each number from A can be used at most once. Your algorithm must have O(nk) time complexity.

  • Using the pseudocode answer these questions Algorithm 1 CS317FinalAlgorithm (A[O..n-1]) ito while i<n - 2 do...

    Using the pseudocode answer these questions Algorithm 1 CS317FinalAlgorithm (A[O..n-1]) ito while i<n - 2 do if A[i]A[i+1] > A[i+2) then return i it i+1 return -1 6. Use limits to show that, for best case inputs, the asymptotic growth of the number of comparisons is (1). Show your work. 7. Use limits to show that, for worst case inputs, the asymptotic growth of the number of comparisons is O(n). Show your work.

  • (C programming) Given a sequence of numbers a1, a2, a3, ..., an, find the maximum sum...

    (C programming) Given a sequence of numbers a1, a2, a3, ..., an, find the maximum sum of a contiguous subsequence of those numbers. Note that, a subsequence of one element is also a contiquous subsequence. Input The input consists of multiple datasets. Each data set consists of: n a1 a2 . . an You can assume that 1 ≤ n ≤ 5000 and -100000 ≤ ai ≤ 100000. The input end with a line consisting of a single 0. Output...

  • Question 1: Complexity Take a look at the following algorithm written in pseudocode: procedure mystery(a1, a2,...

    Question 1: Complexity Take a look at the following algorithm written in pseudocode: procedure mystery(a1, a2, …, an: integer) i := 1 while (i < n and ai ≤ ai+1) i := i + 1 if i == n then print “Yes!” else print “No!” What property of the input sequence {an} does this algorithm test? What is the computational complexity of this algorithm, i.e., the number of comparisons being computed as a function of the input size n? Provide...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT