Check if two People Starting from different Points Ever Meet


What comes to your mind on reading the title which says `Check if two people starting from different points ever meet`? Let’s decode.

We can utilize the idea of relative velocity to find whether two persons coming from different spots ever cross paths.

Now, you might be aware of the term `relative velocity`. Let’s recall. The velocity of an object relative to another item or frame of reference is known as its relative velocity. It is calculated by deducting one object's velocity from another object's velocity.

If two individuals are going in the same direction, their relative velocities can be calculated by rel_vel= abs(vel1−vel2). They will never cross paths if the relative velocity is greater than zero and is in the same direction as one of the individuals. They will eventually cross paths if the relative velocity is in the opposite direction.

If two people are approaching one another, their velocities can be added to get their relative velocities rel_vel= vel1+vel2. They will collide if the relative velocity is greater than zero and is moving in the same direction as one of the individuals. They will pass one another without coming into contact if the relative velocity is in the other direction.

Since the concept of relative velocity and how it will be used makes some sense now, let’s move to the next step.

Approach

Now, let’s convert the logic discussed above into step−by−step approach we will use in our code.

  • First, we take user input for the distance b/w each person and the meeting points. We also take the speed of each person as input.

  • The first case is then implemented in the function where the two people are moving in the same direction.

    • We check if one person's starting position is less than the other person's starting position, and if the first person's velocity is less than or equal to the velocity of the second person (or if the positions and velocities are swapped).

    • If yes , then the two people will never meet in this case, we return false.

  • Then, in the function, we implement the second case, in which the two people are moving towards each other. We compute the difference between the two people's starting positions and their velocities

    • Check if the distance is divisible by the velocities.

    • If yes, the two people will eventually meet, so we return true.

  • Based on the return we print the output to the console.

C++ Code Implementation

Too much theory without any code? Let’s do some code now.

Here is the C++ code to check if two people starting from different points ever meet.

Example

#include <iostream>
using namespace std;

bool doTheyMeet(int x1, int v1, int x2, int v2) {
    // Case 1: If they are moving in the same direction
    if ((x1 < x2 && v1 <= v2) || (x1 > x2 && v1 >= v2)) {
        return false;
    }

    // Case 2: If they are moving towards each other
    if ((x1 - x2) % (v2 - v1) == 0) {
        return true;
    } else {
        return false;
    }
}

int main() {
    int x1 = 0, v1 = 5; //starting position and velocity of 1st person
    int x2 = 6, v2 = 8; //starting position and velocity of 2nd person
    

    if (doTheyMeet(x1, v1, x2, v2)) {
        cout << "They will meet at some point in time." << endl;
    } else {
        cout << "They will never meet." << endl;
    }

    return 0;
}

Output

They will never meet.

Space Complexity: O(1)

Time Complexity: O(1)

Conclusion

In this article, we have covered a detailed explanation of the logic behind the calculation to check if two people starting from different points ever meet. Apart from the logic, we have also covered the approach and C++ code implementation for the same. Hope you have a clear idea of the concept now.

Updated on: 23-Aug-2023

65 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements