Time until distance gets equal to X between two objects moving in opposite direction

Consider two people moving in opposite direction with speeds U meters/second and V meters/second respectively. The task is to find how long will take to make the distance between them X meters.

Examples:

Input: U = 3, V = 3, X = 3
Output: 0.5
After 0.5 seconds, policeman A will be at distance 1.5 meters
and policeman B will be at distance 1.5 meters in the opposite direction
The distance between the the two policemen is 1.5 + 1.5 = 3

Input: U = 5, V = 2, X = 4
Output: 0.571429

Approach: It can be solved using distance = speed * time. Here, distance would be equal to the given range i.e. distance = X and speed would be the sum of the two speeds because they are moving in the opposite direction i.e. speed = U + V.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to return the time for which
// the two policemen can communicate
double getTime(int u, int v, int x)
{
    double speed = u + v;
  
    // time = distance / speed
    double time = x / speed;
    return time;
}
  
// Driver code
int main()
{
    double u = 3, v = 3, x = 3;
    cout << getTime(u, v, x);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach
class GFG 
{
  
    // Function to return the time for which
    // the two policemen can communicate
    static double getTime(int u, int v, int x) 
    {
        double speed = u + v;
  
        // time = distance / speed
        double time = x / speed;
        return time;
    }
  
    // Driver code
    public static void main(String[] args) 
    {
        int u = 3, v = 3, x = 3;
        System.out.println(getTime(u, v, x));
    }
}
  
/* This code contributed by PrinciRaj1992 */

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the approach 
  
# Function to return the time 
# for which the two policemen 
# can communicate 
def getTime(u, v, x): 
  
    speed = u +
  
    # time = distance / speed 
    time = x / speed 
    return time 
  
# Driver code 
if __name__ == "__main__":
  
    u, v, x = 3, 3, 3
    print(getTime(u, v, x)) 
  
# This code is contributed
# by Rituraj Jain

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach
using System;
  
class GFG 
{
  
    // Function to return the time for which
    // the two policemen can communicate
    static double getTime(int u, int v, int x) 
    {
        double speed = u + v;
  
        // time = distance / speed
        double time = x / speed;
        return time;
    }
  
    // Driver code
    public static void Main() 
    {
        int u = 3, v = 3, x = 3;
        Console.WriteLine(getTime(u, v, x));
    }
}
  
// This code is contributed
// by Akanksha Rai

chevron_right


PHP

Output:

0.5


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.