Minimum swaps required to get K cars reach the destination on time
Last Updated :
15 Dec, 2022
Given N number of cars and an integer D i.e distance of the destination. All the cars start from the same starting point moving towards the same destination point. The speeds of each of the cars are given by an array speed[], also their respective positions in increasing order are given in an array position[]. A car going at a slower speed than a previous one is considered as an obstacle for another. The task is to get K number of cars to reach the destination with a minimum number of swaps in a given time T. A car can only be swapped with the car just behind it, this can be done with one car only.
More than one swap at the same time, but each swap shall be counted as a separate swap. If K number of cars cannot reach the destination in given time then return -1
Examples:
Input: N = 5, K= 3, D = 10, T = 5, position[] = {0, 2, 5, 6, 7}, speed[] = {1, 1, 1, 1, 4}
Output: 0
Explanation: No swaps would be required in this case as the speeds of all cars are in increasing order and last 3 cars can easily reach the destination in time T
Input: N = 5, K = 3, D = 10, T = 5, position[] = {0, 2, 3, 5, 7}, speed[] = {2, 1, 1, 1, 4}
Output: 2
Explanation: The car that has covered 0 distance i.e. at (0th index) is swapped with the car at 1st and 2nd index because none of them could reach destination on time. After swapping atleast 3 cars can reach the destination on time.
Input: N = 5, K = 3, D = 10, T = 5, position[] = {0, 2, 3, 4, 7}, speed[] = {2, 1, 1, 1, 4}
Output: -1
Explanation: K number of cars could not reach destination on time by making any number of swaps.
Approach: The task can be solved using greedy approach. Since, the positions of each car are given in increasing order, the best way is to start iterating through the last car, keeping a counter reachable for cars that can reach the destination in time, and a counter obstacle for the cars that can not reach the destination in time. If a car can reach in time then add the number of obstacles to the no of swaps.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int reachables( int x[], int v[], int n,
int & swaps, int d,
int t, int k)
{
int reachable = 0;
int obstacle = 0;
int i = n - 1;
while (i >= 0) {
int temp = d - x[i];
if (v[i] * t >= temp) {
reachable++;
swaps += obstacle;
}
else {
obstacle++;
}
if (reachable >= k) {
break ;
}
i--;
}
if (reachable >= k)
return swaps;
else
return -1;
}
int main()
{
int n = 5, k = 3, d = 10, t = 5;
int x[] = { 0, 2, 3, 5, 7 };
int v[] = { 2, 1, 1, 1, 4 };
int swaps = 0;
cout << reachables(x, v, n, swaps, d, t, k);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int reachables( int x[], int v[], int n,
int swaps, int d,
int t, int k)
{
int reachable = 0 ;
int obstacle = 0 ;
int i = n - 1 ;
while (i >= 0 ) {
int temp = d - x[i];
if (v[i] * t >= temp) {
reachable++;
swaps += obstacle;
}
else {
obstacle++;
}
if (reachable >= k) {
break ;
}
i--;
}
if (reachable >= k)
return swaps;
else
return - 1 ;
}
public static void main(String [] args)
{
int n = 5 , k = 3 , d = 10 , t = 5 ;
int x[] = { 0 , 2 , 3 , 5 , 7 };
int v[] = { 2 , 1 , 1 , 1 , 4 };
int swaps = 0 ;
System.out.println(reachables(x, v, n, swaps, d, t, k));
}
}
|
C#
using System;
public class GFG
{
static int reachables( int []x, int []v, int n,
int swaps, int d,
int t, int k)
{
int reachable = 0;
int obstacle = 0;
int i = n - 1;
while (i >= 0) {
int temp = d - x[i];
if (v[i] * t >= temp) {
reachable++;
swaps += obstacle;
}
else {
obstacle++;
}
if (reachable >= k) {
break ;
}
i--;
}
if (reachable >= k)
return swaps;
else
return -1;
}
public static void Main( string [] args)
{
int n = 5, k = 3, d = 10, t = 5;
int []x = { 0, 2, 3, 5, 7 };
int []v = { 2, 1, 1, 1, 4 };
int swaps = 0;
Console.WriteLine(reachables(x, v, n, swaps, d, t, k));
}
}
|
Python3
def reachables(x, v, n,swaps, d, t, k) :
reachable = 0 ;
obstacle = 0 ;
i = n - 1 ;
while (i > = 0 ) :
temp = d - x[i];
if (v[i] * t > = temp) :
reachable + = 1 ;
swaps + = obstacle;
else :
obstacle + = 1 ;
if (reachable > = k) :
break ;
i - = 1 ;
if (reachable > = k) :
return swaps;
else :
return - 1 ;
if __name__ = = "__main__" :
n = 5 ; k = 3 ; d = 10 ; t = 5 ;
x = [ 0 , 2 , 3 , 5 , 7 ];
v = [ 2 , 1 , 1 , 1 , 4 ];
swaps = 0 ;
print (reachables(x, v, n, swaps, d, t, k));
|
Javascript
<script>
function reachables(x, v, n, swaps, d, t, k) {
let reachable = 0;
let obstacle = 0;
let i = n - 1;
while (i >= 0) {
let temp = d - x[i];
if (v[i] * t >= temp) {
reachable++;
swaps += obstacle;
} else {
obstacle++;
}
if (reachable >= k) {
break ;
}
i--;
}
if (reachable >= k) return swaps;
else return -1;
}
let n = 5,
k = 3,
d = 10,
t = 5;
let x = [0, 2, 3, 5, 7];
let v = [2, 1, 1, 1, 4];
let swaps = 0;
document.write(reachables(x, v, n, swaps, d, t, k));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...