algorithms

2.a) Complexity: O(n)^{2}

algorithms

2. (15 pts) Suppose we are ardent an invest A of unadorned hoard prices control a point hoard. We are asked to subsidize hoard at some spani and fragility it at a advenient span j i, such that twain AL]> Ali] and the selfidentical gain of Av] Ali] is as abundant as likely Control illustration, suffer A = [7, 3, 4, 2, 15, 11, 16, 7, 18, 9, 11, 10]. If we subsidize hoard at span i = 3 with A[i] = 2 and retail at tine J 8 with ALi-18, we frame the maxinum gain

2.a) Entanglement: O(n)^{2}

Description: Span to enact secret loop from 0 to n = O(n)

Span to enact external loop = O(n)

Overtotal entanglement = O(n)*O(n) = O(n)^{2}

2.b) In brace situations when the invest has total identical mass [2,2,2,2,2]’

or, when invest is leisure, it succeed recompense the lapse estimate which is zero

2.c) minNumberSoFar A:

B[0] = A[0];

control i = 1 to i<length(A){

if A[i] < B[i-1]{

B[i] = A[i]

}

else{

B[i] = B[i-1] // B[i-1] already has partiality enumerate tend i-1th locate, if the next enumerate is smaller than the foregoing fragilityection admit it else admit the foregoing smallest enumerate

}

}

recompense B;

Entanglement = O(n), as there is singly single loop from 0 to n

2.d) frameMaxProfitInHindsight A:

B = minNumberSoFar (A) // Using the process created in 2.c

maxProfitSoFar = 0

control i=0 to i<= Length(A)-1{

gain = A[i]-B[i] // Gain succeed be adapted between the ordinary atom and the partiality atom so far

if(maxProfitSoFar<profit){

maxProfitSoFar = gain

}

}

recompense maxProfitSoFar

As there is singly single loop, span entanglement is O(n)