{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

01 - Solution

# 01 - Solution - The Hong Kong University of Science...

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

The Hong Kong University of Science & Technology COMP 271: Design and Analysis of Algorithms Fall 2007 Tutorial 1 (Solution) Below are the solutions of the problems of the tutorial. Question 1 : Follow the example of Lecture 2 and illustrate the MCS algo- rithm with the following input: 4 -3 5 -2 -1 2 6 -2 Solution: 1

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

View Full Document
Question 2 : Suppose you are given an increasing sequence of n distinct in- tegers in an array A [1 ..n ]. Design an efficient algorithm to determine whether there exists an index i such that A [ i ] = i . Justify the correctness of your algorithm and analyze its running time. Solution: Idea of the algorithm The algorithm starts from the middle of the array A and check if we have found a match (i.e. A [ mid ] = mid ). If not, we check to see if what we’ve got is greater or lesser than the required value. If it is greater (i.e. A [ mid ] > mid ), then we can confidently ignore all the values greater than and including the mid point. If it is smaller (i.e. A [ mid ] < mid ), then we can ignore all the values smaller than and including the mid point. This
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 3

01 - Solution - The Hong Kong University of Science...

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

View Full Document
Ask a homework question - tutors are online