Can anyone help in finding o(n) or o(n log n) solution for this particular problem. Thanks in advance :)
Our Chef is opening a new restaurant in the city. Today being the inauguration night of his new restaurant
and Chef’s huge popularity has attracted a large crowd for the grand opening. For convenience
of the customers, cars of all the customers have been marked with a number from 0 to N-1 where N is the
total number of cars, corresponding to their parking space number in which each car has to be parked.
Due to the large crowd, the car parking is full except for one parking space. Moreover, as the cars came too frequently, they could not be parked in their respective parking space. Poor valet at the restaurant’s parking, Raka, is left with the arduous task of parking the cars at their respective places.
Luckily for him, parking has been closed and no more cars are coming and he can now arrange the cars in
their proper places. As Raka is left alone to park the cars, he can only move one car from one parking to the
other parking. He can use the empty parking space to move the cars aroung. He wants to arrange the cars in
as few moves as possible. Raka asks you for help in finding the optimal strategy to arrange cars in their proper
places.
Sample Input File:
2
3
1 0 2
4
2 1 3 0
Output:
3
4