Algorithm MyAlgorithm (A,B) Input:
Arrays A and B each storing n >= 1 integers.
Output: What is the output? (Refer to part b below)
Start: count = 0
C = 10 for i = 0 to C do
{
sum = 0 for j = 0 to n-1 do {
sum = sum + A[0]
for k = 1 to j do
sum = sum + A[k]
}
if B[i] == sum then count = count + 1
}
return count
Document a hand-run on MyAlgorithm for input arrays A = [9 2 5 1] and B = [40 29 2 57] and show the final output.
Initially the values of n is 4
Arrays A = [9 2 5 1] and B = [40 29 2 57]
count = 0 and C = 10
C = 10
for i = 0
sum = 0
for j = 0
sum = sum + 9 =0+9=9
for k = 1 to 0 do // condition fail
so sum is 9
for j = 1
sum = sum + 9 =9+9=18
for k = 1 to 1 do
k=1:
sum = sum + A[1] = 18 + 2 = 20
so sum is 20
for j = 2
sum = sum + 9 =20+9=29
for k = 1 to 2 do
k=1:
sum = sum + A[1] = 29 + 2 = 31
k=2:
sum = sum + A[2] = 31 + 5 = 36
so sum is 36
for j = 3
sum = sum + 9 =36+9=45
for k = 1 to 3 do
k=1:
sum = sum + A[1] = 45 + 2 = 47
k=2:
sum = sum + A[2] = 47 + 5 = 52
k=3:
sum = sum + A[3] = 52 + 1 = 53
so sum is 53
for j = 4 // condition fail
if 40 == 53 // condition fail
now i=1
Repeat the same process
Final comparisons as follows
if 29 == 53 // condition fail
if 2 == 53 // condition fail
if 57 == 53 // condition fail
Therefore the count is 0.
return 0
Algorithm MyAlgorithm (A,B) Input: Arrays A and B each storing n >= 1 integers. Output: What...
The convolution C of two arrays of integers A and B, where A and B are of size n, is defined as follows: C[k] = ∑ A[i] *B[j] where the sum is over all possible values of i and j such that k=( i+j) mod n , and i,j,k= 0,1,2……n-1. For example if n=3, C[0]= A[0]*B[0]+ A[1]*B[2] +A[2]*B[1] C[1]= A[0]*B[1] + A[1]*B[0] + A[2]*B[2] C[2]= A[0]*B[2] + A[1]*B[1] + A[2]*B[0] Write an algorithm with quadratic time complexity for computing C.
Algorithm ComparisonCountingSorting(A[0..n-1], S[0..n-1] ) //Input: Array A[0..n-1] of orderable values //Output: Array S[0..n-1] of A's values in ascending order for i ← 0 to n – 1 do Count[i] ← 0 for i ← 0 to n – 2 do for j ← i + 1 to n – 1 do if A[i] < A[j] Count[j] ← Count[j] + 1 // increment counter of larger else Count[i] ← Count[i] + 1 for i ← 0 to n –...
(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 an input of k arrays each of size n. Each one of the k arrays is sorted. Give an algorithm that turns the k sorted arrays into one sorted array of k * n elements. Please explain the algorithm in words and analyze the run time.
1. Consider the following well-known sorting algorithm, which is studied later in the book, with a counter inserted to count the number of key comparisons. ALGORITHM SortAnalysis(A[0..n − 1]) //Input: An array A[0..n − 1] of n orderable elements //Output: The total number of key comparisons made count ←0 for i ←1 to n − 1 do v ←A[i] j ←i − 1 while j ≥ 0 and A[j ]> v do count ←count + 1 A[j + 1]←A[j ]...
Describe an algorithm that takes as input a list of n integers and produces output the smallest integer in the list.
Consider the following algorithm. ALGORITHM Enigma(A[0.n - 1]) //Input: An array A[0.n - 1] of integer numbers for i leftarrow 0 to n - 2 do for j leftarrow i +1 to n - 1 do if A[i] = = A[j] return false return true a) What does this algorithm do? b) Compute the running time of this algorithm.
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...
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 |...
ALGORITHM PROBLEM: A) Significant Inversions: We are given a sequence of n arbitrary but distinct real numbers <a1 , a2 ,..., an>. We define a significant inversion to be a pair i < j such that ai > 2 aj . Design and analyze an O(n log n) time algorithm to count the number of significant inversions in the given sequence. [Hint: Use divide-&-conquer. Do the “combine” step carefully] B) The Maximum-Sum Monotone Sub-Array Problem: Input: An array A[1..n] of...