Time until distance gets equal to X between two objects moving in opposite direction Last Updated : 22 Jun, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 two policemen is 1.5 + 1.5 = 3Input: 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++ // 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; } Java // 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 */ Python3 # Python3 implementation of the approach # Function to return the time # for which the two policemen # can communicate def getTime(u, v, x): speed = u + v # 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 C# // 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 PHP <?php // PHP implementation of the approach // Function to return the time for which // the two policemen can communicate function getTime($u, $v, $x) { $speed = $u + $v; // time = distance / speed $time = $x / $speed; return $time; } // Driver code $u = 3; $v = 3; $x = 3; echo getTime($u, $v, $x); // This code is contributed // by Akanksha Rai ?> JavaScript <script> // JavaScript implementation of the approach // Function to return the time for which // the two policemen can communicate function getTime(u, v, x) { let speed = u + v; // time = distance / speed let time = x / speed; return time; } // Driver code let u = 3, v = 3, x = 3; document.write(getTime(u, v, x)); // This code is contributed by Surbhi Tyagi. </script> Output: 0.5 Time Complexity: O(1)Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article How to Calculate the Distance Between Two Points? A ayushgoyal Follow Improve Article Tags : DSA Time and Distance Similar Reads Check if it is possible to travel all points in given time by moving in adjacent four directions Given 3 arrays X[], Y[], and T[] all of the size N where X[i] and Y[i] represent the i-th coordinate and T[i] represents the time in seconds. Find it is possible to reach all the coordinates (X[i], Y[i]) in time T[i] from starting coordinates (0, 0). The pointer can move in four directions ( x +1, y 13 min read Distance Traveled by Two Trains together in the same Direction Given two arrays A[] and B[], each consisting of N integers, containing the speeds of two trains travelling in the same direction, at each time unit, the task is to find the total distance travelled by the two trains together(side by side) throughout the journey. Examples: Input: A[] = {1, 2, 3, 2, 5 min read How to Calculate the Distance Between Two Points? Answer: To calculate the distance between Two Points, Distance Formula is used, which is d = \sqrt{[(x_2 - x_1 )^2 +(y_2 - y_1)^2]}The length of the line segment connecting two points is defined as the distance between them. The length of the line segment connecting the specified coordinates can be 6 min read Program to calculate distance between two points You are given two coordinates (x1, y1) and (x2, y2) of a two-dimensional graph. Find the distance between them.Examples: Input : x1, y1 = (3, 4) x2, y2 = (7, 7)Output : 5Input : x1, y1 = (3, 4) x2, y2 = (4, 3)Output : 1.41421Calculate the distance between two points.We will use the distance formula 2 min read POTD Solutions | 02 Novâ 23 | Minimum distance between two numbers View all POTD Solutions Welcome to the daily solutions of our PROBLEM OF THE DAY (POTD). We will discuss the entire problem step-by-step and work towards developing an optimized solution. This will not only help you brush up on your concepts of Arrays but will also help you build up problem-solving 5 min read Time Difference between given times in HH:MM:SS format Given 2 times 'st' and 'et' in HH: MM: SS format. The task is to print the time difference between st and et in HH:MM: SS format Examples: Input: st = 13:50:45, et = 14:55:50Output: 01:05:05Explanation: The time gap is 1 hour 5 minutes and 5 seconds. Input: st = 12:00:00, et = 24:00:00Output: 12:00: 6 min read Like