Question: 4 (15 points) You are given two lists: a1, a2. Each list ai: [ei1,..., ein] ( i = 1, 2) is a sorted list of

4 (15 points) You are given two lists: a1, a2. Each list ai: [ei1,..., ein] ( i = 1, 2) is a sorted list of n numbers. Design an algorithm to find the interval [,] such that The width of the interval is as small as possible and Each list has at least one element which lies within the interval [,] . Example a1 = [ 1, 4, 8, 9, 14, 15, 18 ] a2 = [ 5, 10, 19, 23] The smallest possible interval is [4,5] . Note that each array has one element in this interval a1 has 4, a2 has the element 5. Other possible answers are [9,10] or [18,19] : both intervals contain at least one element from each list and are also of width 1 . Hint Modify the merge algorithm for two sorted arrays as the starting point for designing your algorithm. Merge algorithm maintains an index i in a1 and index j in a2. At each step, if a1[i] < a2[j] it increments i or else it increments j. Consider the intervals [a1[i], a2[j]] at each step. Figure out how to use these intervals to solve your problem. def findMinContainingInterval(a1, a2): # Assume a1, a2 are sorted # Return a tuple (lo, hi) of the interval. assert len(a1) > 0 assert len(a2) > 0 # your code here in python code

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Accounting Questions!