Navigation X
ALERT
Click here to register with a few steps and explore all our cool stuff we have to offer!



 1577

Please help with DSA/Coding problem (will share the prize)

by cyphur23719 - 14 May, 2022 - 06:37 PM
This post is by a banned member (cyphur23719) - Unhide
213
Posts
5
Threads
3 Years of service
#1
(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
  • 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]
Approach
  • Move car 1 from position 2 to position 3. It would take one move. After this state would be: [-1, 0, 1, 2].
So, the answer is 1.


INPUT FORMAT
  • The first line contains 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 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.

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
This post is by a banned member (Steadfast2201) - Unhide
53
Posts
3
Threads
2 Years of service
#2
I can do will met at discord @Ramzes.
Or telegram @Steadfast2201.

Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
or
Sign in
Already have an account? Sign in here.


Forum Jump:


Users browsing this thread: 1 Guest(s)