Problem 1

for all , so . There does not exist such that for all greater than some , since as , so .

For , , so . Thus, for ,

Since , we have .

For , . Thus, .


Problem 2

compares the medians and of and . Denote the result of merging and by . If , it is clear that will be the median of . Consider the case when . Every element in has at least elements in that are greater than it, and thus cannot be the median of . Symmetrically, no element of can be the median of . Since the two chunks are of equal size, the median of is equal to the median of .

The recurrence relation for is given by

Since we are halving the array size at each step, the algorithm will terminate after recursive calls. At each step of the recursion, we only perform operations. Therefore, .

The median of an even length array is considered to be its left median. All elements are distinct. We use sentinels and for all lists . Lists constructed with indices out of order (like ) are empty lists. is the function from part ; it runs in log time.

Consider lists such that . I claim that

  1. No element in can be the median of . Indeed, every element in is less than elements in , and it is easily verified that for all positive .
  2. No element in can be the median of ; every element is greater than elements in . Again, we have for all positive .

Thus, we can safely prune the first elements from and the last elements from . If , it must be because . In this case, it is easy to see that can be the median only if and are even and . If that is not the case, we can discard (note that is always nonzero), and reduce the problem to the one solved in .

It is evident that the algorithm must reach its base case in time, which is then solved in time. Thus, it runs in time.

The algorithm assumes , , , . All elements of and are distinct.

Let denote the number of elements of that occur at or before index in the merged array . The possible values of is restricted by . The algorithm performs binary search for on the subarray .

In each iteration, is selected such that .

  1. If and
    1. , then , and since is the greatest element in , it follows that and .
    2. , then the index of in is strictly greater than . This implies must be strictly lesser than . We proceed with the binary search by discarding the right half.
  2. Similar analysis for .

The algorithm must terminate since we know a solution exists and our search space shrinks geometrically with every iteration. When we have narrowed down the list of possible ‘s to a singleton, the algorithm guarantees that that value () must be equal to . For , the checks specified above pass, and is returned.

The algorithm runs in time.


Problem 3

performs a binary search on : If is positive, either is the last positive element in (in which case is ), or the last positive element in lies in ; In either case, its position is given by . If is negative, the last positive element must lie in , and hence its position is given by . follows the recurrence

so .

bounds the position of the last positive element in a range of length , and then uses , which now runs in time. Finding the range takes time, thus runs in time.


Problem 4

If the elements have a total order, one can sort them and check for duplicates with a linear scan in time.

If we also know bounds on the elements of , and space is not a constraint, this can be achieved in time, discounting the time taken to initialize (once done, it can be used to run the algorithm several times on different inputs until overflows, so the linear time claim isn’t all that unreasonable):


Problem 5

Part a

Using the master theorem, we have

Part b

Part c

Use substitution.

Part d

Substitution, again.

Part e

Assume and . Then,

Thus, .


Problem 6

The recurrences for the three algorithms are given by

where and .

Let for all . Let be the largest integer such that . Let be the maximum value of for .

, so .

It has been shown that algorithm is .

Algorithm has the best time complexity, since for all .


Problem 7

first sorts and . Assume a solution exists, say . At every iteration, either is incremented, or is decremented. Since the algorithm only terminates if either or exceeds bounds, we can conclude that one of these two states must be achieved on some iteration:

  1. and , or
  2. and .

In the first case, will be greater than , resulting in being decremented repeatedly until and the algorithm terminates positively. In the second case, will be lesser than , resulting in being incremented repeatedly until and the algorithm terminates positively.

If a solution does not exist, the loop will terminate in no more than iterations.

Sorting and takes time, and searching for a solution takes time. Thus, the algorithm runs in time.