23.11.2014 Views

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

Data Structures and Algorithms in Java[1].pdf - Fulvio Frisone

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Given a set A = {a 1 ,a 2 , …a n } of n <strong>in</strong>tegers, describe, <strong>in</strong> pseudo-code, an<br />

efficient method for comput<strong>in</strong>g each of partial sums s k =<br />

n. What is the runn<strong>in</strong>g time of this method?<br />

for k= 1,2,…,<br />

C-4.14<br />

Draw a visual justification of Proposition 4.3 analogous to that of Figure 4.1(b)<br />

for the case when n is odd.<br />

C-4.15<br />

An array A conta<strong>in</strong>s n − 1 unique <strong>in</strong>tegers <strong>in</strong> the range [0,n − 1] , that is, there is<br />

one number from this range that is not <strong>in</strong> A. Design an O(n)-time algorithm for<br />

f<strong>in</strong>d<strong>in</strong>g that number. You are only allowed to use O(1) additional space besides<br />

the array A itself.<br />

C-4.16<br />

Let s be a set of n l<strong>in</strong>es <strong>in</strong> the plane such that no two are parallel <strong>and</strong> no three<br />

meet <strong>in</strong> the same po<strong>in</strong>t. Show, by <strong>in</strong>duction, that the l<strong>in</strong>es <strong>in</strong> s determ<strong>in</strong>e Θ(n 2 )<br />

<strong>in</strong>tersection po<strong>in</strong>ts.<br />

C-4.17<br />

Show that the summation<br />

is O(nlogn).<br />

C-4.18<br />

An evil k<strong>in</strong>g has n bottles of w<strong>in</strong>e, <strong>and</strong> a spy has just poisoned one of them.<br />

Unfortunately, they don't know which one it is. The poison is very deadly; just<br />

one drop diluted even a billion to one will still kill. Even so, it takes a full<br />

month for the poison to take effect. Design a scheme for determ<strong>in</strong><strong>in</strong>g exactly<br />

which one of the w<strong>in</strong>e bottles was poisoned <strong>in</strong> just one month's time while<br />

expend<strong>in</strong>g O(logn) taste testers.<br />

C-4.19<br />

Suppose that each row of an n × n array A consists of 1's <strong>and</strong> 0's such that, <strong>in</strong><br />

any row of A all the 1's come before any 0's <strong>in</strong> that row. Assum<strong>in</strong>g A is already<br />

<strong>in</strong> memory, describe a method runn<strong>in</strong>g <strong>in</strong> O(n) time (not O(n 2 ) time) for f<strong>in</strong>d<strong>in</strong>g<br />

the row of A that conta<strong>in</strong>s the most 1's.<br />

C-4.20<br />

256

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!