0% found this document useful (0 votes)
41 views3 pages

El 5 Azo 2

The document describes an algorithm to find a missing integer in an unsorted array of consecutive integers between 1 and n, except for one missing integer. It first presents a divide-and-conquer algorithm that uses deterministic selection to find the median element and partition the array around it, recursively searching either the left or right subarray depending on its size. This runs in O(n) time. It then considers the problem when integers are stored as b-bit binary numbers that can only be accessed by looking up individual bits. The algorithm examines each bit position from least to most significant, counting the number of 0s and 1s to determine the bit of the missing number, reducing the search space in half each iteration
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views3 pages

El 5 Azo 2

The document describes an algorithm to find a missing integer in an unsorted array of consecutive integers between 1 and n, except for one missing integer. It first presents a divide-and-conquer algorithm that uses deterministic selection to find the median element and partition the array around it, recursively searching either the left or right subarray depending on its size. This runs in O(n) time. It then considers the problem when integers are stored as b-bit binary numbers that can only be accessed by looking up individual bits. The algorithm examines each bit position from least to most significant, counting the number of 0s and 1s to determine the bit of the missing number, reducing the search space in half each iteration
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

6.046J/18.

410J Practice Quiz 1 Name 11

Problem 5. Finding the Missing Number [20 points]


Suppose you are given an unsorted array of all integers in the range  to except for one integer,
denoted the missing number. Assume VH R  .
(a) Design a


 
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.

M ISSING I NTEGER (  $ ),( ) !S


1 Determine median element  in range S DKD'D 

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


by the Master Method.



Common errors included using a randomized, instead of deterministic, partitioning
scheme and using C OUNTING S ORT and then stepping through the array to find adja-
cent pairs that differ by two, which is not a Divide and Conquer approach.
6.046J/18.410J Practice Quiz 1 Name 12

(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  

This loop invariant ensures the correctness of the algorithm.


  N

OT
Each loop iteration (Step 5-19) makes B IT-L OOKUP operations. And the size of is halved in
each iteration. Hence total number of B IT-L OOKUP operations is    , which is .


 
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,  .

and  containing  elements  . 


4 Partition the input array around  into  containing  elements 
QR R
5 If S   , then return  .
6 S
Else if  , D ETERMINISTIC S ELECT (    ). S
7 S
Else if  , D ETERMINISTIC S ELECT (   LR R
  SRU  
- ).

You might also like