upenn 502-04 - CIS 502 Spring 2004 1 Solutions to Final...

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

View Full Document Right Arrow Icon
CIS 502 - Spring 2004 (5/3/04) 1 Solutions to Final Exam May 3, 2004 Problem 1. You are given an unsorted array A of n positive integers (may not be distinct). Design an O ( n ) time algorithm to find the smallest positive integer that does not occur in the array A . ( 12 points ) Solution: The smallest missing positive integer is at most n +1 since there are at most n distinct integers in A . Initialize a Boolean array M [1 .. ( n + 1)] to false. Scan A [1] through A [ n ], and for each A [ i ] n + 1, set M [ A [ i ]] to true to indicate that A [ i ] is not a candidate. Now output the least integer j such that M [ j ] is false. Since both A and M are of size O ( n ), the algorithm runs in O ( n ) time. Problem 2. Design a data structure that maintains a dynamic set S whose elements are distinct positive integers and supports the following operations: (a) Insert ( x ) and Delete ( x ); (b) Range ( x, y ): number of elements in S whose value lies strictly between x and y ; and (c) Sum ( x, y ): sum up the elements in S whose value lies strictly between x and y . Note that the elements x and y above are any two positive integers that may or may not be in the set S . You can assume that x y . All operations should run in O (log n ) worst-case time where n denotes the number of elements in the set. You can build upon any data structure described in the textbook or in the class. ( 18 points ) Solution: We maintain the set S by modifying the order statistics tree data structure to include in each node x an additional field sum [ x ]. The field contains the sum of the values of the elements in the subtree rooted at x (including x itself). We maintain the sum information similarly to size without affecting the asymptotic running time for insert and delete operations. On insert, instead of adding 1 along the path traversed from the root down to the leaves, we add the value of the inserted element. When rotation is performed, we update the corresponding size fields using the information from the subtrees and the key values of the two nodes involved. Deletion is performed analogously. We define a function OS-Sum ( x ) that returns the sum of the elements x . Its implementation is analogous to OS-Rank .
Background image of page 1

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

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 11/26/2009 for the course CIS 502 taught by Professor Naver during the Spring '09 term at National Tsing Hua University, China.

Page1 / 4

upenn 502-04 - CIS 502 Spring 2004 1 Solutions to Final...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online