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, 4}, B[] = {2, 1, 3, 1, 4}
Output: 3
Explanation :
Since A[1] + A[0] = B[0] + B[1], both the trains have travelled same distance after 2 units of time.
Now, since A[2] = B[2] = 3, both the trains have traveled this distance together.
After the 3rd unit of time, the speed of the trains are different.
Therefore, the total distance traveled by the two trains together is 3.
Input: A[] = {1, 1, 3, 2, 4}, B[] = {3, 1, 2, 1, 4}
Output:
Approach:
Follow the steps below to solve the problem:
- Traverse both the arrays simultaneously.
- For every ith index check if sum(A[0] .. A[i – 1]) is equal to sum(B[0] .. B[i – 1]) as well as if A[i] and B[i] are equal or not.
- If the above two conditions are satisfied, add A[i] to the answer.
- Finally, after traversal of the complete array, print answer.
Below is the implementation of above approach:
C++
// C++ Program to find the distance // traveled together by the two trains #include <iostream> using namespace std; // Function to find the distance traveled together int calc_distance( int A[], int B[], int n) { // Stores distance travelled by A int distance_traveled_A = 0; // Stores distance travelled by B int distance_traveled_B = 0; // Stores the total distance // travelled together int answer = 0; for ( int i = 0; i < 5; i++) { // Sum of distance travelled distance_traveled_A += A[i]; distance_traveled_B += B[i]; // Condition for traveling // together if ((distance_traveled_A == distance_traveled_B) && (A[i] == B[i])) { answer += A[i]; } } return answer; } // Driver Code int main() { int A[5] = { 1, 2, 3, 2, 4 }; int B[5] = { 2, 1, 3, 1, 4 }; int N = sizeof (A) / sizeof (A[0]); cout << calc_distance(A, B, N); return 0; } |
Java
// Java program to find the distance // traveled together by the two trains import java.util.*; import java.lang.*; class GFG{ // Function to find the distance traveled together static int calc_distance( int A[], int B[], int n) { // Stores distance travelled by A int distance_traveled_A = 0 ; // Stores distance travelled by B int distance_traveled_B = 0 ; // Stores the total distance // travelled together int answer = 0 ; for ( int i = 0 ; i < 5 ; i++) { // Sum of distance travelled distance_traveled_A += A[i]; distance_traveled_B += B[i]; // Condition for traveling // together if ((distance_traveled_A == distance_traveled_B) && (A[i] == B[i])) { answer += A[i]; } } return answer; } // Driver code public static void main (String[] args) { int A[] = { 1 , 2 , 3 , 2 , 4 }; int B[] = { 2 , 1 , 3 , 1 , 4 }; int N = A.length; System.out.println(calc_distance(A, B, N)); } } // This code is contributed by offbeat |
Python3
# Python3 program to find the distance # traveled together by the two trains # Function to find the distance # traveled together def calc_distance(A, B, n): # Stores distance travelled by A distance_traveled_A = 0 # Stores distance travelled by B distance_traveled_B = 0 # Stores the total distance # travelled together answer = 0 for i in range ( 5 ): # Sum of distance travelled distance_traveled_A + = A[i] distance_traveled_B + = B[i] # Condition for traveling # together if ((distance_traveled_A = = distance_traveled_B) and (A[i] = = B[i])): answer + = A[i] return answer # Driver Code A = [ 1 , 2 , 3 , 2 , 4 ] B = [ 2 , 1 , 3 , 1 , 4 ] N = len (A) print (calc_distance(A, B, N)) # This code is contributed by sanjoy_62 |
C#
// C# program to find the distance // traveled together by the two trains using System; class GFG{ // Function to find the distance // traveled together static int calc_distance( int []A, int []B, int n) { // Stores distance travelled by A int distance_traveled_A = 0; // Stores distance travelled by B int distance_traveled_B = 0; // Stores the total distance // travelled together int answer = 0; for ( int i = 0; i < 5; i++) { // Sum of distance travelled distance_traveled_A += A[i]; distance_traveled_B += B[i]; // Condition for traveling // together if ((distance_traveled_A == distance_traveled_B) && (A[i] == B[i])) { answer += A[i]; } } return answer; } // Driver Code public static void Main( string []s) { int []A = { 1, 2, 3, 2, 4 }; int []B = { 2, 1, 3, 1, 4 }; int N = A.Length; Console.Write(calc_distance(A, B, N)); } } // This code is contributed by rutvik_56 |
Javascript
<script> // javascript program to find the distance // traveled together by the two trains // Function to find the distance traveled together function calc_distance(A , B , n) { // Stores distance travelled by A var distance_traveled_A = 0; // Stores distance travelled by B var distance_traveled_B = 0; // Stores the total distance // travelled together var answer = 0; for (i = 0; i < 5; i++) { // Sum of distance travelled distance_traveled_A += A[i]; distance_traveled_B += B[i]; // Condition for traveling // together if ((distance_traveled_A == distance_traveled_B) && (A[i] == B[i])) { answer += A[i]; } } return answer; } // Driver code var A = [ 1, 2, 3, 2, 4 ]; var B = [ 2, 1, 3, 1, 4 ]; var N = A.length; document.write(calc_distance(A, B, N)); // This code is contributed by aashish1995 </script> |
3
Time Complexity: O(N)
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!