# Difference between revisions of "Chapter 5"

Jump to navigation Jump to search

# Divide and Conquer

### Binary Search

5.1. Suppose you are given a sorted array ${\displaystyle A}$ of size n that has been circularly shifted ${\displaystyle k}$ positions to the right. For example, [35, 42, 5, 15, 27, 29] is a sorted array that has been circularly shifted ${\displaystyle k=2}$ positions, while [27, 29, 35, 42, 5, 15] has been shifted ${\displaystyle k=4}$ positions.
• Suppose you know what ${\displaystyle k}$ is. Give an ${\displaystyle O(1)}$ algorithm to find the largest number in ${\displaystyle A}$.
• Suppose you do not know what ${\displaystyle k}$ is. Give an ${\displaystyle O(lgn)}$ algorithm to find the largest number in ${\displaystyle A}$. For partial credit, you may give an ${\displaystyle O(n)}$ algorithm.

5.2. A sorted array of size n contains distinct integers between ${\displaystyle 1}$ and ${\displaystyle n+1}$ , with one element missing. Give an ${\displaystyle O(logn)}$ algorithm to find the missing integer, without using any extra space.

5.3 Consider the numerical Twenty Questions game. In this game, the first player thinks of a number in the range 1 to ${\displaystyle n}$. The second player has to figure out this number by asking the fewest number of true/false questions. Assume that nobody cheats.
(a) What is an optimal strategy if ${\displaystyle n}$ in known?
(b) What is a good strategy if ${\displaystyle n}$ is not known?

5.4. You are given a unimodal array of ${\displaystyle n}$ distinct elements, meaning that its entries are in increasing order up until its maximum element, after which its elements are in decreasing order. Give an algorithm to compute the maximum element of a unimodal array that runs in ${\displaystyle O(logn)}$ time.

5.5. Suppose that you are given a sorted sequence of distinct integers ${\displaystyle [a_{1},a_{2},...,a_{n}]}$. Give an ${\displaystyle O(lgn)}$ algorithm to determine whether there exists an index ${\displaystyle i}$ such that ${\displaystyle a_{i}=i}$. For example, in [-10, -3, 3, 5, 7], ${\displaystyle a_{3}=3}$. In [2, 3, 4, 5, 6, 7], there is no such ${\displaystyle i}$.

5.6. Suppose that you are given a sorted sequence of distinct integers ${\displaystyle a=[a_{1},a_{2},...,a_{n}]}$, drawn from 1 to ${\displaystyle m}$ where ${\displaystyle n. Give an ${\displaystyle O(lgn)}$ algorithm to find an integer ${\displaystyle \leq m}$ that is not present in ${\displaystyle a}$. For full credit, find the smallest such integer ${\displaystyle x}$ such that ${\displaystyle 1\leq x\leq m}$.

5.7. Let ${\displaystyle M}$ be an ${\displaystyle n*m}$ integer matrix in which the entries of each row are sorted in increasing order (from left to right) and the entries in each column are in increasing order (from top to bottom). Give an efficient algorithm to find the position of an integer ${\displaystyle x}$ in ${\displaystyle M}$, or to determine that ${\displaystyle x}$ is not there. How many comparisons of ${\displaystyle x}$ with matrix entries does your algorithm use in worst case?

### Divide and Conquer Algorithms

5.8. Given two sorted arrays ${\displaystyle A}$ and ${\displaystyle B}$ of size ${\displaystyle n}$ and ${\displaystyle m}$ respectively, find the median of the ${\displaystyle n+m}$ elements. The overall run time complexity should be ${\displaystyle O(log(m+n))}$.

5.9. The largest subrange problem, discussed in Section 5.6, takes an array ${\displaystyle A}$ of ${\displaystyle n}$ numbers, and asks for the index pair ${\displaystyle i}$ and ${\displaystyle j}$ that maximizes ${\displaystyle S=\sum _{k=1}^{j}A[k]}$. Give an ${\displaystyle O(n)}$ algorithm for largest subrange.

5.10. We are given ${\displaystyle n}$ wooden sticks, each of integer length, where the ${\displaystyle i}$th piece has length ${\displaystyle L[i]}$. We seek to cut them so that we end up with ${\displaystyle k}$ pieces of exactly the same length, in addition to other fragments. Furthermore, we want these ${\displaystyle k}$ pieces to be as large as possible.
(a) Given four wood sticks, of lengths ${\displaystyle L={10,6,5,3}}$, what are the largest sized pieces you can get for ${\displaystyle k=4}$? (Hint: the answer is not 3).
(b) Give a correct and efficient algorithm that, for a given ${\displaystyle L}$ and ${\displaystyle k}$, returns the maximum possible length of the ${\displaystyle k}$ equal pieces cut from the initial ${\displaystyle n}$ sticks.

5.11. Extend the convolution-based string-matching algorithm described in the text to the case of pattern matching with wildcard characters “*”, which match any character. For example, “sh*t” should match both “shot” and “shut”.

### Recurrence Relations

5.12

5.13

5.14

5.15

5.16

Back to Chapter List