8. Consider the following algorithm, which finds the sum of all of the integers in a...
8. [10 points) Consider the following algorithm procedure Algorithm(: integer, n: positive integer; 81,...a s integers with vhilei<r print (l, r, mı, arn, 》 if z > am then 1:= m + 1 if za then anstwer-1 return answer 18 and the (a) Assume that this algorithm receives as input the numbersz-32 and corresponding sequence of integers 2 | 3 1 1 4151617| 8| 9 | 10 İ 11 İ 12 | 13 | 14|15 | 16 | 17 |...
(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...
Consider the following problem: Input: a list of n-1 integers and these integers are in the range of 1 to n. There are no duplicates in list. One of the integers from 1 to n is missing in the list. Output: find the missing integer Let the input array be [2, 4, 1, 6, 3, 7, 8]. Elements in this list are in the range of 1 to 8. There are no duplicates, and 5 is missing. Your algorithm needs...
(a) Prove the following loop invariant by induction on the number of loop iterations: Loop Invariant: After the kth iteration of the for loop, total = a1 + a2 + · · · + ak and L contains all elements from a1 , a2 , . . . , ak that are greater than the sum of all previous terms of the sequence. (b) Use the loop invariant to prove that the algorithm is correct, i.e., that it returns a...
9. (5 points) Please describe an algorithm that takes as input a list of n integers and finds the number of negative integers in the list. 10. (5 points) Please devise an algorithm that finds all modes. (Recall that a list of integers is nondecreasing if each term of the list is at least as large as the preceding term.) 11. (5 points) Please find the least integer n such that f() is 0(3") for each of these functions f()...
9. [10 points) Consider the following algorithm: procedure Algorithm(n: positive integer; ddd: distinet integers) for k:=1 to n-1 for 1-1 to n-k print(k, I, di,da...-1,dn) if ds dti then interchange dy and d (a) Assume that this algorithm receives as input the integer n 6 and the input sequence 하하하하하하, Miss ^-ruteae rehen i12|3141516 Fill out the table below: ds ds (b) Assume that the algorithm receives the same input values as in part a). Once the algorithm finishes, what...
Problem 3: (5 2 points) Design an algorithm that takes an array of positive integers A of length n and a positive integer T as an input and finds the largest N < T such that N can be written as a sum of some elements of A and returns such a representation of N. The complexity of the algorithms has to be O(nT). For example, for A 3,7, 10 and T 19, the output is 17 7+10, because we...
Subset Sum-2 Write an algorithm (in comments) and specify the big O, and a C program to solve the problern below. Read the input for the set elements, the value of K from the user. Assume the size of the set is not bigger than 20. Subset Sum-3 Write an algorithm (in comments) and specify from the user. Assume the size of the set is not bigger than 20 1. Given a finite set of integers, is there a subset...
17. Consider the following algorithm: procedure Algorithm(n: positive integer; di,d2.. ,dn: distinct integers) for 1 to n-1 for 1 to n-k if ddi+ then interchange di and di+ print(k, I, d,ddn-1, dn) (a) |3 points Assume that this algorithm receives as input the integer-6 and the corresponding input sequence 41 36 27 31 17 20 Fill out the table below ds (b) 1 point Assume that the algorithm receives the same input values as in part a). Once the algo-...
Consider the following algorithm that operates on a list of n integers: • Divide the n values into n 2 pairs • Find the max of each pair. • Repeat until you have the max value of the list (a) Show the steps of the above algorithm for the list (25,19,9,8,2,26,21,26,31,26,3,14). (b) Derive and prove a tight bound on the asymptotic runtime of this algorithm (c) Assuming you just ran the above algorithm, show that you can use the result...