Question:
A parking space hasn+1 parking spots numbered from 0 to n. There are n cars numbered from 1 to n. Cars are parked in random order. You have to rearrange cars such that car i is at parking spot i and parking spot 0 becomes empty. You can move one car at a time. Give minimum number of times you move the car to arrange the cars in correct order.
For example:
parking size with 4 slots. Empty spot is represented by -1.
parking: [3, 2, -1, 1] With 3 operations they can be rearranged as parking: [-1, 1, 2, 3]
1. With 1st opration Move car 2 to 2, parking becomes: [3, -1, 2, 1]
2. With 2nd operation move car 1 to 1, parking becomes: [3, 1, 2, -1]
3. With 3rd operation move car 3 to 3, parking becomes: [-1, 1, 2, 3]What is the optimal solution for the problem