Re: midterm for 94.404 at UMass Lowell
- From: TheGist <fake@xxxxxxxxxxxx>
- Date: Thu, 25 May 2006 23:54:51 -0400
David Wagner wrote:
TheGist wrote:Consider a set S of n>=2 distinct numbers given in unsorted order. c) In O(n) time, determine x,y in S such that x+y=Z, where Z is given or determine that no two such numbers exist.
Ok, you got me. How do you solve this one in O(n) time?
Basically, go through the list of numbers and create a hash table
key is Z, value is (x,y). Hash Table creation is O(n)
Now, given Z look it up in the has tbale for the appropriate, if any
x and y. Hash Table lookup is O(1). Thus, in total O(n).
.
- Follow-Ups:
- Re: midterm for 94.404 at UMass Lowell
- From: David Wagner
- Re: midterm for 94.404 at UMass Lowell
- References:
- midterm for 94.404 at UMass Lowell
- From: TheGist
- Re: midterm for 94.404 at UMass Lowell
- From: David Wagner
- midterm for 94.404 at UMass Lowell
- Prev by Date: Re: midterm for 94.404 at UMass Lowell
- Next by Date: Re: midterm for 94.404 at UMass Lowell
- Previous by thread: Re: midterm for 94.404 at UMass Lowell
- Next by thread: Re: midterm for 94.404 at UMass Lowell
- Index(es):
Relevant Pages
|
|