Open In App

Minimum swaps required to get K cars reach the destination on time

Last Updated : 15 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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++




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to get minimum
// number of swaps and returns
// number of cars that
// car reach there in time.
int reachables(int x[], int v[], int n,
               int& swaps, int d,
               int t, int k)
{
 
    // Counter for number of cars that
    // can reach in time
    int reachable = 0;
 
    // Counter for cars that cannot reach
    // destination in time.
    int obstacle = 0;
 
    int i = n - 1;
    while (i >= 0) {
 
        int temp = d - x[i];
        if (v[i] * t >= temp) {
            // If a car can reach in
            // time then increase the
            // reachable counter
            reachable++;
 
            // Adding the number of cars
            // that need to be swapped
            swaps += obstacle;
        }
        else {
            // If a car cannot reach
            // in time then increase
            // the obstacle counter
            obstacle++;
        }
 
        if (reachable >= k) {
            break;
        }
 
        i--;
    }
    // Return the swaps
    if (reachable >= k)
        return swaps;
    else
        return -1;
}
// Driver Code
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




// Java program for the above approach
import java.io.*;
class GFG
{
  // Function to get minimum
  // number of swaps and returns
  // number of cars that
  // car reach there in time.
  static int reachables(int x[], int v[], int n,
                 int swaps, int d,
                 int t, int k)
  {
 
      // Counter for number of cars that
      // can reach in time
      int reachable = 0;
 
      // Counter for cars that cannot reach
      // destination in time.
      int obstacle = 0;
 
      int i = n - 1;
      while (i >= 0) {
 
          int temp = d - x[i];
          if (v[i] * t >= temp) {
              // If a car can reach in
              // time then increase the
              // reachable counter
              reachable++;
 
              // Adding the number of cars
              // that need to be swapped
              swaps += obstacle;
          }
          else {
              // If a car cannot reach
              // in time then increase
              // the obstacle counter
              obstacle++;
          }
 
          if (reachable >= k) {
              break;
          }
 
          i--;
      }
      // Return the swaps
      if (reachable >= k)
          return swaps;
      else
          return -1;
  }
   
  // Driver Code
  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));
 
  }
 
}
 
// This code is contributed by AR_Gaurav


C#




// C# program for the above approach
using System;
public class GFG
{
   
  // Function to get minimum
  // number of swaps and returns
  // number of cars that
  // car reach there in time.
  static int reachables(int []x, int []v, int n,
                 int swaps, int d,
                 int t, int k)
  {
 
      // Counter for number of cars that
      // can reach in time
      int reachable = 0;
 
      // Counter for cars that cannot reach
      // destination in time.
      int obstacle = 0;
 
      int i = n - 1;
      while (i >= 0) {
 
          int temp = d - x[i];
          if (v[i] * t >= temp) {
              // If a car can reach in
              // time then increase the
              // reachable counter
              reachable++;
 
              // Adding the number of cars
              // that need to be swapped
              swaps += obstacle;
          }
          else {
              // If a car cannot reach
              // in time then increase
              // the obstacle counter
              obstacle++;
          }
 
          if (reachable >= k) {
              break;
          }
 
          i--;
      }
      // Return the swaps
      if (reachable >= k)
          return swaps;
      else
          return -1;
  }
   
  // Driver Code
  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));
  }
}
 
// This code is contributed by AnkThon


Python3




# Python3 program for the above approach
 
# Function to get minimum
# number of swaps and returns
# number of cars that
# car reach there in time.
def reachables(x, v, n,swaps, d, t, k) :
 
    # Counter for number of cars that
    # can reach in time
    reachable = 0;
 
    # Counter for cars that cannot reach
    # destination in time.
    obstacle = 0;
 
    i = n - 1;
    while (i >= 0) :
 
        temp = d - x[i];
        if (v[i] * t >= temp) :
            # If a car can reach in
            # time then increase the
            # reachable counter
            reachable += 1;
 
            # Adding the number of cars
            # that need to be swapped
            swaps += obstacle;
             
        else :
            # If a car cannot reach
            # in time then increase
            # the obstacle counter
            obstacle += 1;
 
        if (reachable >= k) :
            break;
 
        i -= 1;
         
    # Return the swaps
    if (reachable >= k) :
        return swaps;
    else :
        return -1;
 
# Driver Code
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));
 
    # This code is contributed by AnkThon


Javascript




<script>
// Javascript program for the above approach
 
// Function to get minimum
// number of swaps and returns
// number of cars that
// car reach there in time.
function reachables(x, v, n, swaps, d, t, k) {
  // Counter for number of cars that
  // can reach in time
  let reachable = 0;
 
  // Counter for cars that cannot reach
  // destination in time.
  let obstacle = 0;
 
  let i = n - 1;
  while (i >= 0) {
    let temp = d - x[i];
    if (v[i] * t >= temp) {
      // If a car can reach in
      // time then increase the
      // reachable counter
      reachable++;
 
      // Adding the number of cars
      // that need to be swapped
      swaps += obstacle;
    } else {
      // If a car cannot reach
      // in time then increase
      // the obstacle counter
      obstacle++;
    }
 
    if (reachable >= k) {
      break;
    }
 
    i--;
  }
  // Return the swaps
  if (reachable >= k) return swaps;
  else return -1;
}
// Driver Code
 
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));
 
// This code is contributed by saurabh_jaiswal.
</script>


Output

2

Time Complexity: O(N)
Auxiliary Space: O(1)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads