{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

assignment1_sol

# assignment1_sol - else L=1 R=m while(L<=R i=floor(L R/2...

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

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

View Full Document
else L=1; R=m; while (L<=R) i=floor((L+R)/2); j=k-i; if (j=n or A[i]<=B[j+1]) and (i=m or B[j]<=A[i+1]) then return max(A[i],B[j]); /* A[1..i], B[1..j] contains the smallest k items */ else if A[i]>B[j+1] then R=i-1; /* rank(A[i]) > k, pick a smaller i */ else if B[j]>A[i+1] then L=i+1; /* rank(B[j]) > k, pick a smaller j and a bigger i */ end if end while end if end 2. Let X be a list of n objects X 1 ,X 2 , · · · ,X n . Construct a list Y of at most n 2 objects such that X 2 i 1 Y if and only if X 2 i 1 = X 2 i for i = 1 , 2 , · · · , n 2 . Note that Y does not cover X n when n is odd. Suppose more than half of the the objects in X are equal to x . (a) When n is even, the number of x in X is bigger than n 2 = n 2 . When n is odd and x negationslash = X n , the number of x in X is bigger than n 1 2 = n 2 In both cases, there must exists i such that X 2 i 1 = X 2 i = x . So x Y . (b) Consider any object y in Y such that the number of y in Y is | Y | 2 . Since for each pair of objects X 2 i 1 ,X 2 i such that X 2 i 1 negationslash = X 2 i , at most one of them can be equal to y . The number of y in X is at most | Y | 2
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 8

assignment1_sol - else L=1 R=m while(L<=R i=floor(L R/2...

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

View Full Document
Ask a homework question - tutors are online