## Description

Professor Brito

CS 3510 Staff

Instructions.

For the graded problems, you are allow to use the following theorems and algorithm from classes as black-boxes. In doing so, you do not need to explain your algorithm. Clearly state what is your input and how you use the output. You can report their runtime as presented in class with no further explanation.

โข Master Theorem, QuickSort, MergeSort and the Merging subroutine, FastSelect (median of medians).

Suggested reading.

We are covering topics from Chapter 2. You do not need to cover topics with havenโt cover in lectures.

Suggested Problems.

All problems are from Chapter 2. The numeration corresponds to the 2006 edition of the book.

You do not need to turn this problem in.

โข 2.5 (to use the MAster Theorem directly. Some parts of this problem are more general recurrence we wonโt see in the class but you are welcome to explore.)

โข 2.14 (deleting duplicates).

โข 2.15 (implementing in place).

โข 2.17 (fixed point).

โข 2.19 (k-merge).

โข 2.20 (sorting a bounded array).

โข 2.22 (kth element of the union of two sorted lists).

โข 2.23 (majority element).

Practice problem.

Problem 2.16 (finding x in an infinite array.)

Problem 1

You are given a list of n bits {x1,x2,…,xn} with each xi โ {0,1}. You have to output either: โข A natural number K such that xK = 1, or

โข 0 if all bits are equal to zero.

The only operation you are allow to access the inputs is a function I(i,j) defined as:

(

1, if some bit in xi,xi+1,…,xj has value 1; I(i,j) =

0, if all bits xi,xi+1,…,xj have value 0.

The function I(ยท,ยท) runs in constant time.

Design a divide and conquer algorithm to solve this problem. Describe your algorithm with words (no pseudocode) and justify its correctness. Analyze and justify its running time. Make sure to include the recurrence relation.

Problem 2

You are given an array A = [a1,…,an] of n positive numbers that represent the price of a particular stock on n days. You want to buy the stock at one day and sell on a later day. Your goal is to determine what would have been the best pair of days for buying/selling. You simply have to output the difference in price. You can assume n is a power of 2. Here is an example: A = [10,15,6,3,7,12,2,9]. Then the optimal decision would be to purchase on day 4 for $3 and sell on day 6 for $12, so your algorithm should output $9.

(a) Suppose you want to buy in the first n/2 days and you want to sell in the last n/2 days. Give an O(n) time algorithm for finding the best pair of days for buying/selling under this restriction. Explain your algorithm in words and analyse its running time. You can use any approach you prefer, in particular, you do not have to use divide and conquer.

(b) Give a divide and conquer algorithm with running time O(nlogn) for finding the best pair of days for

## Reviews

There are no reviews yet.