Computer Science Homework Solutions
Problem
#70941

explanation of solution


The solution seems to be the the following, but I need a more in-depth explanation, particularly on why we should use merge sort and binary sort.

Here is the problem at a high level (you can look at the attachment for more detail):
Describe a (n log_2 n) time algorithm that, given a set S of n real numbers
and another real number x, determines whether or not there exist two
elements in S whose sum is exactly x.


Solution:
Sort the array using an algorithm of order O(n log n) (You can use mergesort but
you cannot use quicksort)
Then for every element y in the array binary search the sorted array for x - y

O(n log n) + O(n log n) = O(n log n)

Attached file(s):
Attachments
sum-of-2-numbers.pdf  View File

Attachment Content Summary (Note: view attachment at the above link before purchasing. Actual attachment content may vary slightly from that shown below.)

sum-of-2-numbers.pdf
Lubo and Mike are building a "do all" robot in CS148 and have found that
they have to fit two lego pieces side by side into an opening. The opening is
x inches wide and the sum of the widths of the two pieces has to be exactly
equal to the width of the opening, otherwise, their robot will fall apart during
the demo. They have a huge collection of lego pieces of varying widths at their
disposal and have to pick two pieces which satisfy the above constraint.
Since the Computer Science department at Brown believes in doing every-
thing through a well defined algorithm, you should supply these poor guys an
algorithm for doing the task, and one that is asymptotically efficient! So, here
goes your exact problem.
You are given a set of n real numbers and another real number x. Describe
a O(n log n)-time algorithm that determines whether or not there exist two
elements in S whose sum is exactly x.
Hint : Doesn't the O(n log n) term make you feel that sorting is involved in
some way?
Solution
What is this?
By OTA - Overall OTA Rating
Xiao Liu, MS - 4.7/5
Purchase Cost Now
$2.19 CAD (was ~$15.96)
Included in Download
  • Plain text response
$2.19 Instant Download
Add to Cart
Why you can trust BrainMass.com
  • Your Information is Secure
  • Best Online Academic Help Service
  • Students find real academic Success
Related Solutions
Browse