this is the question ans solution of the question. I want to have the another al
ID: 3806184 • Letter: T
Question
this is the question ans solution of the question. I want to have the another algorithm that do the same of this aglorithm below without changing in the running time O(n log n)
It is an exam question, so please do not answer this question if you are not sure about your answer.
Let A = (x1, . . . , xn) be an array storing n numbers, and let [a1, b1], . . . , [an, bn] be n intervals
with 1 ai bi n, for all 1 i n. Your task is to develop an algorithm which, for every
interval [ai, bi], computes the value mi = min{xj | ai j bi}. Note that your algorithm has to
compute all values m1,m2, . . . ,mn at the same time. The running time of your algorithm should
be O(n lg n). Do not analyze your algorithm and do not prove its correctness.
RANGEMINIMA(A, Q, i, r) A is the array; Q contains the query All. r is the subarray on which the current recursive call operates. 1 if Q 2 then return immediately 3 if 1 r 4 then for every interval I E Q do output Allj as the answer else q Alq Sl q for i q 2 downto l do slil min (sli 11, Ali]) 10 for i q 1 to r do plij min pli 1], Ali) 11 12 13 for every interval la, b e Q 14 do if a q and b q 15 16 then OUtput min(s[a], p[b]) as the answer 17 else if then Qu Q u a, b] 18 else Qr Qr U a, b 19 RANGEMINIMA (A, Qu, i, q 1) RANGEMINIMA A O g r)Explanation / Answer
It is very simple, sort the array using merge sort which will be done in O(nlogn) in worst case and run n queries on sorted array. Sorted will return minimum value in O(1) in given range.
sort(A)
for i <- 1 to n
q <- q[i]
return A(ai)//in sorted array always ai will contain minimum value
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.