This story is about an alternate timeline of Avengers Endgame.
Thanos becomes inevitable in this timeline by acquiring all infinity stones. Now Thanos wants to kill all the universe. Thanos launched an attack to kill all the world at once by snapping his fingers. The world is now going to explode in ‘time’ hours.
To save the world, Avengers decided to time travel using time quantum. There are ‘N’ avengers running towards the time quantum. Each Avenger is running with some speed from some initial position. If an Avenger fails to reach the ‘time quantum’, then he/she will die.
Doctor Strange wants to save as many Avengers as possible by swapping initial positions of Avengers adjacent to each other.
Doctor Strange went ahead of time and saw all 1,44,89,121 results of the Thanos vs Avengers battle. He found that he needs to save at least ‘K’ Avengers to defeat Thanos.
Doctor Strange has very little energy left to save the world by doing the minimum number of swaps.
Doctor strange is busy assembling all the Avengers, so he asked you to help him.
The fate of the world lies in your hand.
Note :The initial positions of all the Avengers are sorted in increasing order and no two Avengers have the same initial position.
If an Avenger crosses the other Avenger, they will travel together with Avenger’s speed moving ahead i.e. with the speed of slower Avenger.
The first line of input contains an integer 'T' representing the number of test cases. Then the test cases follow.
The first line of each test case contains four space-separated integers ‘N’, ‘K’, ‘destination’ and ‘time’ representing the total number of Avengers, required number of Avengers to be saved, the position of time quantum, and the time remaining to destroy the world in hours.
The next line contains ‘N’ single-spaced elements sorted in increasing order, representing Avenger's initial positions.
The next line contains ‘N’ single-spaced elements, representing Avengers speed in km/hour.
Output Format :
For each test case, print the minimum number of swaps required to save at least ‘K’ Avengers. Print -1 if it is impossible to save ‘K’ Avengers.
The output of each test case will be printed in a separate line.
Note :
You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 5
1 <= K <= N <= 5000
1 <= destination, time <= 10^9
1 <= Speed[i] , Position[i] <= 10^9
Where Position[i] is the initial position of the Avengers and Speed[i] is the initial speed of the Avengers.
Time Limit: 1 sec
2
4 3 10 3
1 2 3 4
4 8 6 3
3 3 10 5
1 4 5
1 5 2
0
-1
Test Case 1 :
Given ‘N’ = 4, ‘K’ = 3, ‘destination’ = 10 and ‘time’ = 3.
Avengers position = [1, 2, 3, 4]
Avengers speed = [4, 8, 6, 3]
Fourth Avenger can easily reach 10 from 4 in 3 hours as he can travel 9 km in 3 hours.
Third Avenger can easily go 10 from 3 in 3 hours as he can travel 18 km in 3 hours. Also, as Third Avenger is running faster than the fourth Avenger, he will cross the fourth Avenger and reach the time quantum together.
Second Avenger can easily reach 10 from 2 in 3 hours as he can travel 24 km in 3 hours. He will cross paths with the third Avenger and reach time quantum together.
We have three Avengers that can reach the time quantum. So we don’t need any swaps.
Test Case 2 :
Given ‘N’ = 3, ‘K’ = 3, ‘destination’ = 10 and ‘time’ = 5.
Avengers position = [1, 4, 5]
Avengers speed = [1, 5, 2]
No matter how many swaps we do, we cannot save at least 3 Avengers.
So we need to print -1.
2
1 1 10 3
1
7
3 2 8 5
1 2 5
8 1 2
0
1
Test Case 1 :
Given ‘N’ = 1, ‘K’ = 1, ‘destination’ = 10 and ‘time’ = 3.
Avengers position = [1]
Avengers speed = [7]
First Avenger can easily reach 10 from 1 in 3 hours as he can travel 21 km in 3 hours.
So we need to print 0
Test Case 2 :
Given ‘N’ = 3, ‘K’ = 2, ‘destination’ = 8 and ‘time’ = 5.
Avengers position = [1, 2, 5]
Avengers speed = [8, 1, 2]
Third Avenger can easily reach 10 from 5 in 5 hours as he can travel 10 km in 5 hours.
Second Avenger cannot reach 10 from 2 in 3 hours as he can travel 5 km in 5 hours.
So we will swap the second Avenger with the first Avenger as the first avenger can reach 10 from 2.
So we need to print 1.
Traverse from right to left and try to save as many Avengers as you can.
The idea here is to traverse the list of Avengers from right to left, and for each Avenger, we will calculate the maximum distance he/she can cover. If this maximum distance is greater than equal to the required distance (distance from the initial position to time quantum), we will increment the count of saved Avengers by 1. Else, we will increase the count of the Avengers which can’t be saved.. If we have encountered an Avenger that can be saved but we have ‘X’ number of Avengers which cannot be saved ahead of him then we will And ‘X’ to our ‘answer’ as we need to bring the Avenger ahead of all Avengers who cannot be saved, for that we need ‘X’ swaps.
Algorithm
O(N), where ‘N’ is the total number of elements in the given array.
Here, we are running a single loop that takes O(N) time.
O(1).
No extra space is being used.