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:3Explanation :

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 i
^{th}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; ` ` ` ` ` `// 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[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; ` `} ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

**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.

## Recommended Posts:

- Distance traveled by Hour and Minute hand in given Time interval
- Calculate the Square of Euclidean Distance Traveled based on given conditions
- Time until distance gets equal to X between two objects moving in opposite direction
- Maximum trains for which stoppage can be provided
- Move matrix elements in given direction and add elements with same value
- Total number of ways to place X and Y at n places such that no two X are together
- Seating arrangement of N boys sitting around a round table such that two particular boys sit together
- Probability of not getting two consecutive heads together in N tosses of coin
- Find if there exists a direction for ranges such that no two range intersect
- Maximum distance between two occurrences of same element in array
- Distance of chord from center when distance between center and another equal length chord is given
- Loss when two items are sold at same price and same percentage profit/loss
- Number of ways to arrange a word such that all vowels occur together
- Minimum Swaps required to group all 1's together
- Minimum swaps required to bring all elements less than or equal to k together
- Check if all occurrences of a character appear together
- Minimum number of adjacent swaps for arranging similar elements together
- Minimum time required to complete a work by N persons together
- Permutations of n things taken r at a time with k things together
- Count numbers have all 1s together in binary representation

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.