给定两个数组A []和B [] ,每个数组由N个整数组成,包含两个列车在相同时间方向上在同一时间行进的速度,任务是找到两个列车一起行进的总距离(侧并排整个过程。
例子:
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:
方法:
请按照以下步骤解决问题:
- 同时遍历两个数组。
- 对于每个第i个索引,检查sum(A [0] .. A [i – 1])是否等于sum(B [0] .. B [i – 1])以及A [i]和B [i]是否相等。
- 如果满足以上两个条件,则将A [i]添加到答案中。
- 最后,遍历整个数组后,输出answer 。
下面是上述方法的实现:
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[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;
// 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
输出:
3
时间复杂度: O(N)
辅助空间: O(1)