Midterm1Sol

Midterm1Sol - COT 5405 Analysis of Algorithms Spring 2010...

This preview shows pages 1–2. Sign up to view the full content.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: COT 5405 Analysis of Algorithms, Spring 2010. Midterm 1 February 18, 2010 1. [ 1 2 page] (a) Given an unsorted array of n numbers, give an efficient algorithm ( O ( n log n )) to determine the existence of a pair of numbers in the array whose sum is equal to a given value M . (b) Find a better algorithm if the array was sorted. Solution: (a) Sort the array A in increasing order, which will take O ( n log n ) time. Then for each element A i , search for M- A i using binary search leading to an overall complexity of O ( n log n ). For the second part, the answer is yes. (b) If the array is already sorted, you can maintain two pointers l and r . Initially l = 1 and r = n . If A l + A r < M , then increment l , else if A l + A r > M then decrement r . Stop when you get A l + A r = M (and return success), or when l > r (and return failure). Time complexity is O ( n ). 2. [1 page] A group of entomologists have collected n insects which are to be grown all together in an insectarium. The insects are of different species, but the entomologists know from prior experience that if there are more than n 2 insects of any one species, then the insects of the other species will be devoured. They need to efficiently determine whether there are indeed more than n 2 insects of any one species from their sample of n insects. Unfortunately, the different species they have collected areone species from their sample of n insects....
View Full Document

{[ snackBarMessage ]}

Page1 / 2

Midterm1Sol - COT 5405 Analysis of Algorithms Spring 2010...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online