Related Articles
Distance Traveled by Two Trains together in the same Direction
• Difficulty Level : Expert
• Last Updated : 31 Mar, 2021

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:
Explanation :
Since A + A = B + B, both the trains have travelled same distance after 2 units of time.
Now, since A = B = 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 .. A[i – 1]) is equal to sum(B .. 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 ``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;` `    ``// Stpres 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 = { 1, 2, 3, 2, 4 };``    ``int` `B = { 2, 1, 3, 1, 4 };``    ``int` `N = ``sizeof``(A) / ``sizeof``(A);` `    ``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``;` `    ``// Stpres 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` `    ``# Stpres 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;` `    ``// Stpres 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

 ``
Output:
`3`

Time Complexity: O(N)
Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up