Problem 3 (25 points)
A certain one-way street has m parking spaces in a row, numbered 1 through m. A
man and his dozing wife drive by, and suddenly she wakes up and orders her
husband to park immediately. He parks at the first available space unless there are
no parking spaces left that he can get to without backing up (i.e., if his wife awoke
when the car approached space k, but spaces k, k+1, ..., m are already occupied) in
which cases he apologizes to her and drives on.
Suppose the above scenario occurs for n different cars, where the $j^{th}$ wife wakes up
just in time to park in space $a_j$. For example when $m = n = 9$ and the $j^{th}$ wife wakes at
$< a_1, a_2, \dots, a_9 > = <3,1,4,1,5,9,2,6,5>$, the cars are parked as follows:
Car
2
4
1
3
5
7
8
9
6
space
1
2
3
4
5
6
7
8
9
Design an efficient algorithm determining in which space, if any, a car parks.