Dadas dos arrays A[] y B[] , cada una de las cuales consta de N enteros, que contienen las velocidades de dos trenes que viajan en la misma dirección, en cada unidad de tiempo, la tarea es encontrar la distancia total recorrida por los dos trenes juntos (lado al lado) a lo largo del viaje.
Ejemplos:
Entrada: A[] = {1, 2, 3, 2, 4}, B[] = {2, 1, 3, 1, 4}
Salida: 3
Explicación:
Dado que A[1] + A[0] = B [0] + B[1], ambos trenes han recorrido la misma distancia después de 2 unidades de tiempo.
Ahora, como A[2] = B[2] = 3, ambos trenes han recorrido esta distancia juntos.
Después de la tercera unidad de tiempo, la velocidad de los trenes es diferente.
Por lo tanto, la distancia total recorrida por los dos trenes juntos es 3.
Entrada: A[] = {1, 1, 3, 2, 4}, B[] = {3, 1, 2, 1, 4}
Salida:
Enfoque:
siga los pasos a continuación para resolver el problema:
- Atraviesa ambas arrays simultáneamente.
- Para cada i -ésimo índice, compruebe si sum(A[0] .. A[i – 1]) es igual a sum(B[0] .. B[i – 1]) , así como si A[i] y B [i] son iguales o no.
- Si se cumplen las dos condiciones anteriores, agregue A[i] a la respuesta .
- Finalmente, después de atravesar la array completa, imprima la respuesta .
A continuación se muestra la implementación del enfoque anterior:
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; }
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
<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; // Stpres 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
Complejidad temporal: O(N)
Espacio auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por thakurabhaysingh445 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA