Question
In this part you have to come up with an algorithm to answer a more complicated range query, namely, given an array A as above,
In this part you have to come up with an algorithm to answer a more complicated range query, namely, given an array A as above, a range query [a, b] now requires you to actually list all of the A[i]s in the range a . . . b. Using the same array A as above, the range query [2, 7] asks which numbers from A are there in the array A are between 2 and 7, so the answer should be 3,5,5,6,7 (it doesnt matter in what order the numbers are outputted). Your algorithm should work within the following time bounds:
The one time preprocessing step should take O(n + k) time (the preprocessing here will be different from that in the first part).
Given a range query [a, b], the time taken to answer this query should not depend on k or on n or on the quantity b a; instead, the query should be answered in time O(t) where t is the number of elements in the output (the number of A[i]s in the range [a, b]). For example, if we consider two range queries, [a1, b1] and [a2, b2], [a1, b1] leads to an output of 500 elements, and [a2, b2] leads to an output of 100 elements, then answering [a1, b1] should take approximately 5 times as much time as answering [a2, b2]. Or consider another example, where k = 106 , n = 105 , a = 2000, b = 3000, and there are only three elements in the range [2000, 3000]. If you look at every entry in C between 2000 and 3000 i.e. you look at C[2000], C[2001], C[2002], . . . , C[3000], you are looking at b a = 1001 entries which is too expensive since t = 3 is much smaller than b 1 i.e. there are only three elements in the range [2000, 3000].
(a) Give a clear description of your algorithm for the above problem. This description should include both how the preprocessing step is carried out, and then how the range queries are answered.
(b) Trace your algorithm on the above example i.e. for the array A as above, show what will happen in the preprocessing step. Then show how the algorithm will answer the range query [2, 7]. Also show how the algorithm will answer the range query [1, 9].
(c) Do a worst case time analysis (use big O notation) of your algorithm i.e. answer the two questions: how much time does the preprocessing step take and how much time does answering the range query take.
********Hint: You will need to keep one or more auxillary arrays; what these are going to be, you need to figure out. You should show how to fill these auxillary array(s) in time O(n + k) (this is the preprocessing step), and once these auxillary array(s) are available, you have to figure out how they can be used to quickly answer the range queries.***************
Pleas note the following information regarding a previous question to the one presented here: "all the entries of the array A are between 1 and k. You want to repeatedly answer range queries of the form [a, b] where 1 a b k; each range query asks how many A[i]s are in the range a . . . b, i.e., how many numbers from A lie between a and b. For example, if k = 10, and the array A has seven elements 5, 9, 3, 5, 10, 6, 1, 7 the range query [2, 7] asks how many numbers are there in the array A between 2 and 7, so the answer should be 5 (because the numbers 3,5,5,6,7 from A lie in the range [2, 7]). You want to do this efficiently. In order to do this, you first do some preprocessing, which is a one-time operation i.e"
Please apply this to the original question.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started