Check if two people starting from different points ever meet
There are two people that start from two different positions, let’s say x1 and x2. Both can jump v1 and v2 meters ahead respectively. We have to find if both will ever meet given that the number of jumps taken by both has to be same.
Print ‘Yes’ if they will meet,
print ‘No’ they will not.
Examples :
Input : x1 = 5, v1 = 8, x2 = 4, v2 = 7 Output : No Explanation: The first person is starting ahead of the second one. and his speed is also greater than the second one, so they will never meet. Input : x1 = 6, v1 = 6, x2 = 4, v2 = 8 Output : Yes
Naive Approach: In this, we calculate the position of each person after each jump and checks if they have landed in the same spot or not. This one having complexity O(n).
C++
// C++ program to find if two people // starting from different positions // ever meet or not. #include <bits/stdc++.h> using namespace std; bool everMeet( int x1, int x2, int v1, int v2) { // If speed of a person at a position before // other person is smaller, then return false. if (x1 < x2 && v1 <= v2) return false ; if (x1 > x2 && v1 >=v2) return false ; // Making sure that x1 is greater if (x1 < x2) { swap(x1, x2); swap(v1, v2); } // Until one person crosses other while (x1 >= x2) { if (x1 == x2) return true ; // first person taking one // jump in each iteration x1 = x1 + v1; // second person taking // one jump in each iteration x2 = x2 + v2; } return false ; } // Driver code int main() { int x1 = 5, v1 = 8, x2 = 4, v2 = 7; if (everMeet(x1, x2, v1, v2)) printf ( "Yes" ); else printf ( "No" ); return 0; } |
Java
// Java program to find // if two people starting // from different positions // ever meet or not. import java.io.*; class GFG { static void swap( int a, int b) { int t = a; a = b; b = t; } static boolean everMeet( int x1, int x2, int v1, int v2) { // If speed of a person // at a position before // other person is smaller, // then return false. if (x1 < x2 && v1 <= v2) return false ; if (x1 > x2 && v1 >= v2) return false ; // Making sure that // x1 is greater if (x1 < x2) { swap(x1, x2); swap(v1, v2); } // Until one person // crosses other while (x1 >= x2) { if (x1 == x2) return true ; // first person taking one // jump in each iteration x1 = x1 + v1; // second person taking // one jump in each iteration x2 = x2 + v2; } return false ; } // Driver code public static void main (String[] args) { int x1 = 5 , v1 = 8 , x2 = 4 , v2 = 7 ; if (everMeet(x1, x2, v1, v2)) System.out.println( "Yes" ); else System.out.println( "No" ); } } // This code is contributed // by akt_mit |
Python3
# Python3 program to find if two # people starting from different # positions ever meet or not. def everMeet(x1, x2, v1, v2): # If speed of a person at # a position before other # person is smaller, then # return false. if (x1 < x2 and v1 < = v2): return False ; if (x1 > x2 and v1 > = v2): return False ; # Making sure that # x1 is greater if (x1 < x2): x1, x2 = x2,x1; v1, v2 = v2,v1; # Until one person # crosses other while (x1 > = x2): if (x1 = = x2): return True ; # first person taking one # jump in each iteration x1 = x1 + v1; # second person taking # one jump in each iteration x2 = x2 + v2; return False ; # Driver code x1 = 5 ; v1 = 8 ; x2 = 4 ; v2 = 7 ; if (everMeet(x1, x2,v1, v2)): print ( "Yes" ); else : print ( "No" ); # This code is contributed by mits |
C#
// C# program to find if two // people starting from different // positions ever meet or not. using System; class GFG { static void swap( ref int a, ref int b) { int t = a; a = b; b = t; } static bool everMeet( int x1, int x2, int v1, int v2) { // If speed of a person at a // position before other person // is smaller, then return false. if (x1 < x2 && v1 <= v2) return false ; if (x1 > x2 && v1 >= v2) return false ; // Making sure that x1 is greater if (x1 < x2) { swap( ref x1, ref x2); swap( ref v1, ref v2); } // Until one person crosses other while (x1 >= x2) { if (x1 == x2) return true ; // first person taking one // jump in each iteration x1 = x1 + v1; // second person taking // one jump in each iteration x2 = x2 + v2; } return false ; } // Driver code static void Main() { int x1 = 5, v1 = 8, x2 = 4, v2 = 7; if (everMeet(x1, x2, v1, v2)) Console.Write( "Yes" ); else Console.Write( "No" ); } } // This code is contributed by // Manish Shaw(manishshaw1) |
PHP
<?php // PHP program to find if two // people starting from different // positions ever meet or not. function everMeet( $x1 , $x2 , $v1 , $v2 ) { // If speed of a person at // a position before other // person is smaller, then // return false. if ( $x1 < $x2 && $v1 <= $v2 ) return false; if ( $x1 > $x2 && $v1 >= $v2 ) return false; // Making sure that // x1 is greater if ( $x1 < $x2 ) { list( $x1 , $x2 ) = array ( $x2 , $x1 ); list( $v1 , $v2 ) = array ( $v2 , $v1 ); } // Until one person // crosses other while ( $x1 >= $x2 ) { if ( $x1 == $x2 ) return true; // first person taking one // jump in each iteration $x1 = $x1 + $v1 ; // second person taking // one jump in each iteration $x2 = $x2 + $v2 ; } return false; } // Driver code $x1 = 5; $v1 = 8; $x2 = 4; $v2 = 7; if (everMeet( $x1 , $x2 , $v1 , $v2 )) echo "Yes" ; else echo "No" ; // This code is contributed by ajit ?> |
Javascript
<script> // Javascript program to find // if two people starting // from different positions // ever meet or not. function swap(a, b) { let t = a; a = b; b = t; } function everMeet(x1, x2, v1, v2) { // If speed of a person // at a position before // other person is smaller, // then return false. if (x1 < x2 && v1 <= v2) return false ; if (x1 > x2 && v1 >= v2) return false ; // Making sure that // x1 is greater if (x1 < x2) { swap(x1, x2); swap(v1, v2); } // Until one person // crosses other while (x1 >= x2) { if (x1 == x2) return true ; // first person taking one // jump in each iteration x1 = x1 + v1; // second person taking // one jump in each iteration x2 = x2 + v2; } return false ; } let x1 = 5, v1 = 8, x2 = 4, v2 = 7; if (everMeet(x1, x2, v1, v2)) document.write( "Yes" ); else document.write( "No" ); </script> |
No
Auxiliary Space: O(1) because it is using constant space for variables
Efficient approach: Other way is to use the concept of Relative Speed. If the X1 is D distance away from X2, then relative speed of X1 should be in factors of D so as both land on the same spot.
Example:
X1 = 10
X2 = 25
V1 = 10
V2 = 8
Since here D = 15 and relative speed = 2 which is not a factor of D they will never meet.
The time complexity of this algorithm is O(1)
Implementation is given below:
C++
// C++ program to find if two people // starting from different positions // ever meet or not. #include <bits/stdc++.h> using namespace std; bool everMeet( int x1, int x2, int v1, int v2) { // If speed of a person at a position before // other person is smaller, then return false. if (x1 < x2 && v1 <= v2) return false ; if (x1 > x2 && v1 >= v2) return false ; // Making sure that x1 is greater if (x1 < x2) { swap(x1, x2); swap(v1, v2); } // checking if relative speed is // a factor of relative distance or not return ((x1 - x2) % (v1 - v2) == 0); } // Driver code int main() { int x1 = 5, v1 = 8, x2 = 4, v2 = 7; if (everMeet(x1, x2, v1, v2)) printf ( "Yes" ); else printf ( "No" ); return 0; } |
Java
// Java program to find if two people // starting from different positions // ever meet or not. public class GFG { static boolean everMeet( int x1, int x2, int v1, int v2) { // If speed of a person at a position before // other person is smaller, then return false. if (x1 < x2 && v1 <= v2) { return false ; } if (x1 > x2 && v1 >= v2) { return false ; } // Making sure that x1 is greater if (x1 < x2) { swap(x1, x2); swap(v1, v2); } // checking if relative speed is // a factor of relative distance or not return ((x1 - x2) % (v1 - v2) == 0 ); } static void swap( int a, int b) { int t = a; a = b; b = t; } public static void main(String[] args) { int x1 = 5 , v1 = 8 , x2 = 4 , v2 = 7 ; if (everMeet(x1, x2, v1, v2)) { System.out.printf( "Yes" ); } else { System.out.printf( "No" ); } } } // This code is contributed by 29AjayKumar |
Python3
# Python3 program to find if two people # starting from different positions # ever meet or not. def everMeet(x1, x2, v1, v2): # If speed of a person at a position before # other person is smaller, then return false. if (x1 < x2 and v1 < = v2): return False ; if (x1 > x2 and v1 > = v2): return False ; # Making sure that x1 is greater if (x1 < x2): swap(x1, x2) swap(v1, v2) # checking if relative speed is # a factor of relative distance or not return ((x1 - x2) % (v1 - v2) = = 0 ) def swap(a, b): t = a a = b b = t # Driver code def main(): x1, v1, x2, v2 = 5 , 8 , 4 , 7 if (everMeet(x1, x2, v1, v2)): print ( "Yes" ) else : print ( "No" ) if __name__ = = '__main__' : main() # This code is contributed by 29AjayKumar |
C#
//C# program to find if two people // starting from different positions // ever meet or not. using System; public class GFG{ static bool everMeet( int x1, int x2, int v1, int v2) { // If speed of a person at a position before // other person is smaller, then return false. if (x1 < x2 && v1 <= v2) { return false ; } if (x1 > x2 && v1 >= v2) { return false ; } // Making sure that x1 is greater if (x1 < x2) { swap(x1, x2); swap(v1, v2); } // checking if relative speed is // a factor of relative distance or not return ((x1 - x2) % (v1 - v2) == 0); } static void swap( int a, int b) { int t = a; a = b; b = t; } public static void Main() { int x1 = 5, v1 = 8, x2 = 4, v2 = 7; if (everMeet(x1, x2, v1, v2)) { Console.WriteLine( "Yes" ); } else { Console.WriteLine( "No" ); } } } // This code is contributed by 29AjayKumar |
PHP
<?php // PHP program to find if two people // starting from different positions // ever meet or not. function everMeet( $x1 , $x2 , $v1 , $v2 ) { // If speed of a person at a position before // other person is smaller, then return false. if ( $x1 < $x2 && $v1 <= $v2 ) return false; if ( $x1 > $x2 && $v1 >= $v2 ) return false; // Making sure that x1 is greater if ( $x1 < $x2 ) { list( $x1 , $x2 ) = array ( $x2 , $x1 ); list( $v2 , $v1 ) = array ( $v1 , $v2 ); } // checking if relative speed is // a factor of relative distance or not return (( $x1 - $x2 ) % ( $v1 - $v2 ) == 0); } // Driver code $x1 = 5; $v1 = 8; $x2 = 4; $v2 = 7; if (everMeet( $x1 , $x2 , $v1 , $v2 )) print ( "Yes" ); else print ( "No" ); // This code is contributed by mits ?> |
Javascript
<script> // Javascript program to find if two people // starting from different positions // ever meet or not. function everMeet(x1, x2, v1, v2) { // If speed of a person at a position before // other person is smaller, then return false. if (x1 < x2 && v1 <= v2) { return false ; } if (x1 > x2 && v1 >= v2) { return false ; } // Making sure that x1 is greater if (x1 < x2) { swap(x1, x2); swap(v1, v2); } // Checking if relative speed is // a factor of relative distance or not return ((x1 - x2) % (v1 - v2) == 0); } function swap(a, b) { let t = a; a = b; b = t; } // Driver code let x1 = 5, v1 = 8, x2 = 4, v2 = 7; if (everMeet(x1, x2, v1, v2)) { document.write( "Yes" ); } else { document.write( "No" ); } // This code is contributed by mukesh07 </script> |
No
Time complexity: O(1)
Auxiliary space: O(1)
Please Login to comment...