OP 14 May, 2022 - 06:37 PM
(This post was last modified: 14 May, 2022 - 06:59 PM by cyphur23719. Edited 3 times in total.)
The world is rapidly changing and getting smarter day by day. To go towards the path of sustainable development, you have decided to build a future park where you have parking space for solar-powered electric vehicles that have 0 emissions and save fuel and the environment. In the future park, the solar-powered cars need to be parked in such a way that they can get just enough sunlight in 1 hour to fully charge themselves.
The current state of car parking is represented by an array Current wherein the array element -1 represents a pillar, 0 represents space, and a positive integer represents the car number at that spot.
You are also given an array Desired which shows the ideal parking positioning of cars so that they can charge themselves fully in 1 hour.
In one move, you are allowed to move a car one place forward or backward to an empty spot.
(pretty much useless text lol)
MAIN -->
TASK
Determine the minimum number of moves required to achieve the desired state of solar-powered cars. If it is not possible, print -1.
NOTES
EXAMPLE
Assumptions
INPUT FORMAT
OUTPUT FORMAT
For each test case in a new line, print the minimum number of moves required or -1 if not possible.
Sample input
1
4
0 1 2 0
1 2 0 0
Sample output
2
The current state of car parking is represented by an array Current wherein the array element -1 represents a pillar, 0 represents space, and a positive integer represents the car number at that spot.
You are also given an array Desired which shows the ideal parking positioning of cars so that they can charge themselves fully in 1 hour.
In one move, you are allowed to move a car one place forward or backward to an empty spot.
(pretty much useless text lol)
MAIN -->
TASK
Determine the minimum number of moves required to achieve the desired state of solar-powered cars. If it is not possible, print -1.
NOTES
- Assume 1- based indexing.
- You cannot move pillars. Also, no cars can fly over another car or go through the pillars.
- Cars are numbered starting from 1 to the number of cars.
EXAMPLE
Assumptions
- N = 4
- Current = [-1, 1, 0, 2]
- Desired = [-1, 0, 1, 2]
- Move car 1 from position 2 to position 3. It would take one move. After this state would be: [-1, 0, 1, 2].
INPUT FORMAT
- The first line contains T denoting the number of test cases. T also specifies the number of times you have to run the solve function on a different set of inputs.
- For each test case:
- The first line contains an integer N denoting the size of the solar-powered car parking.
- The second line contains N space-separated numbers, denoting the current state of parking of solar cars.
- The third line contains N space-separated numbers, representing the desired state of parking solar cars.
- The first line contains an integer N denoting the size of the solar-powered car parking.
OUTPUT FORMAT
For each test case in a new line, print the minimum number of moves required or -1 if not possible.
Sample input
1
4
0 1 2 0
1 2 0 0
Sample output
2