Distance Traveled by Two Trains together in the same Direction

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[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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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


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


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.



Improved By : rutvik_56, offbeat, sanjoy_62