El 5 Azo 2
El 5 Azo 2
Divide and Conquer algorithm to find the missing number. Partial
credit will be given for non Divide and Conquer algorithms. Argue (informally) that
your algorithm is correct and analyze its running time. [12 points]
Solution: We can use S ELECT to find the median element and check to see if it is in
the array. If it is not, then it is the missing number. Otherwise, we PARTITION the
array around the median element into elements and . If the first one has size
less than
, then we recurse on this subarray. Otherwise we recurse on the other
subarray.
!S
The procedure M ISSING I NTEGER ( $
,( ) takes as input an array and a range
!S
$
,( in which the missing number lies.
2 Check to see if is in
3 PARTITION into , elements , and > , elements
>
4 If S IZE ( )
5 M ISSING I NTEGER ( $ ( ) !S
6 Else M ISSING I NTEGER ( $
,( )
The running time is
because the recurrence for this algorithm is
: H
, which is
(b) Suppose the integers in are stored as -bit binary numbers, i.e. each bit is or .
For example, if and the array VH
$ ( , then the missing number is .
Now the only operation to examine the integers is B IT-L OOKUP
, which returns S
the th bit of number $ -( and costs unit time. Design an !S
algorithm to find the
missing number. Argue (informally) that your algorithm is correct and analyze its
running time. [8 points]
Solution: We shall examine bit by bit starting from the least significant bit to the most significant
bit. Make a count of the number of 1’s and 0’s in each bit position, we can find whether the missing
number has a 0 or 1 at the bit position being examined. Having done this, we have reduced the
problem space by half as we have to search only among the numbers with that bit in that position.
We continue in this manner till we have exhausted all the bit-positions (ie., to 1).
The algorithm is as follows. For convenience, we have 3 sets which are maintained as
link-lists. contains the indices of the elements in which we are going to examine while (
) contains the indices of the elements in which have 0 (1) in the bit position being examined.
M ISSING -I NTEGER
J H ?R
1
H KD'DKD
2
3 Initialized to Empty List
4 count0 count1
5 for posn downto 1 do
for each S
6 do
7 bit B IT-L OOKUP posn
S
8 if bit
9 then count0 count0
S
10 Add to
11 else count1 count1
12 Add to S
13 if count0 count1
14 then missing $ posn(
15
16 else missing $ posn(
17
18
19 count0 count1
20 return missing
It can be noted that the following invariant holds at the end of the loop in Step 5-19.
The bits of the missing integer in the bit position (posn to ) is given by
DKD'D
missing $ posn(
CS
missing $ k( .
bit of !S
$ -( missing $ ( for posn
The grading for this problem was (-6) points for an algorithm that runs in , like R ADIX S ORT
7
(or any of a number of built-from-scratch radix-sort-like approaches). Another point was deducted
6.046J/18.410J Practice Quiz 1 Name 13
S
D ETERMINISTIC S ELECT ( )
1 Divide the elements of the input array into groups of elements.
: :
2 Find median of each group of 3 elements and put them in array .
3 Call D ETERMINISTIC S ELECT (
) to find median of the medians, .